我对nodejs真的很陌生,而且主干太混乱了
登录:
在客户端,我使用骨干网。
模型
var testModel = Backbone.Model.extend({
url:'api/test',
defaults:{
login:false,
apikey:{},
},
initialize:function(){
this.bind('change:apikey',this.onApiKeyChange,this);
// this.set({'apikey': localStorage.getItem('apikey')});
},
setApiKey:function(apikey){
localStorage.setItem('apiKey', apikey);
this.set({'apikey':apikey});
},
onApiKeyChange:function(){
// console.log(this.toJSON())
//login here
}
});
看法
var testView = Backbone.View.extend({
el:'#content',
events:{
'submit #loginForm':'login',
},
login:function(e){
e.preventDefault();
this.model.setApiKey({name:this.$('input[name=username]').val(),password:this.$('input[name=password]').val()});
}
})
var test= new testView({model:new testModel()});
因此,当用户提交表单时,模型会发生更改并触发onApiKeyChange()
,如果用户登录onApiKeyChange()
则应使用用户详细信息更新模型。我不知道该怎么做,主干如何发送这样的请求?
服务器端:
var express = require('express');
var routes = require('./routes');
var http = require('http');
var mongoose = require('mongoose');
var app = express();
mongoose.connect('mongodb://localhost/test');
var User = mongoose.model('User',new mongoose.Schema({
name:String,
password:String,
}));
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard cat" }));
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/api/test', someFunction());
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
服务端只是基本的express app.js,不知道session怎么用。
我知道这可能是一个基本问题,但我就是不明白。请给我一些建议或告诉我一些适合初学者的简单教程。