【问题标题】:Accessing the Backbone model.save() POST payload in the node-express backend在 node-express 后端访问 Backbone model.save() POST 有效负载
【发布时间】:2013-10-01 02:58:10
【问题描述】:

我已经使用主干编写了一个代码 sn-p,它将 POST 的数据发送到 urlRoute 。

(function(){
    "use strict"
     window.Course = Backbone.Model.extend({
        defaults:{
            title:''
            },
        urlRoot:"courses/"


        });
    var courses = new Course({title:"Sending a Post request to the node-express backend,but how to access this in the backend"});
    courses.save();

})();

我在后端使用了 node.js - express 框架,我想知道如何使用 app.post('/courses',function(req,res){ 检索 title 属性的值}) 方法。 这是node.js的后端,控制权来到了app.post方法,只是想知道如何访问发布数据中的模型值。

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path');

var app = express();

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.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});


app.post('/courses',function(req,res) { 
        console.log('Request successfully recieved');
    console.log("how do i get the posted data here !!");
});
app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
    }); 

【问题讨论】:

  • 它看起来不完整,您如何将模型存储在服务器端?如果您只想在客户端上列出它们,只需在其上创建一个 Backbone.Collection.add() 模型,然后在客户端上查询,然后您可以在服务器上进行相同的操作,并保持同步集合和模型,这样就可以了所有客户端都一样

标签: node.js backbone.js express


【解决方案1】:

您可以在 req.body 中找到您的请求数据(更多关于 http://expressjs.com/api.html#req.body)。

在你的情况下,你可以这样做:

app.post('/courses',function(req,res) { 
    console.log(req.body.title);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 2020-04-18
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多