2013-10-01 3 views
0

urlRoute에 대한 POST 데이터 인 백본을 사용하여 코드 스 니펫을 작성했습니다.노드 - 익스프레스 백엔드에서 백본 model.save() POST 페이로드 액세스

(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를 - 백엔드에서 명시 적 프레임 워크, 나는 app.post ('/ 과정', 기능 (REQ, 고해상도를 사용하여 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')); 
    }); 
+0

불완전 보이는, 정확히 어떻게 당신은 서버 측의 모델을 저장? 'Backbone.Collection'과'.add()'모델을 작성한 다음 클라이언트에서 쿼리하면 서버에서 똑같은 일을 할 수 있고 컬렉션과 모델을 동기화 할 수 있습니다 , 모든 클라이언트에서 동일 할 수 있습니다. – Gntem

답변

1

당신은 req.body (즉, http://expressjs.com/api.html#req.body에 대한 자세한)에서 요청 데이터를 찾을 수 있습니다. 귀하의 경우에는

다음과 같이 할 수 있습니다

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

감사합니다. 작동합니다 :) –