1
編輯後未定義:我固定它使用:request.body使用快遞bodyParser
app.configure(function(){
app.use(express.bodyParser());
});
原帖:
我試圖弄清楚如何處理與後節點和表達,我完全卡住了。
一些閱讀後,我注意到人們說我應該使用「中間件」知道是什麼意思,並創建一個行app.use(express.bodyParser());
。我認爲在添加後我會在我的發佈方法中使用req.body
。但情況並非如此。它將console.log轉換爲undefined
。
我以爲我不知道如何正確地設置的,所以在這裏不用什麼:
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, UserProvider = require('./userprovider').UserProvider,
qs = require('querystring');
var userProvider = new UserProvider('localhost', 27017);
var app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(8080);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/new_game', function (req, res) {
res.sendfile(__dirname + '/new_game.html');
});
app.post('/new_game', function(req, res) {
var codeToUse = Math.random()*10000;
codeToUse = Math.round(codeToUse);
console.log(req.body);
});
app.use(express.bodyParser());
app.listen(3000);
好各自的崗位路徑文件使用bodyParser我認爲應該在程序的頂部使用中間件。他們在你的計劃和秩序中的位置很重要..不是嗎? –
我已經解決了我自己的問題,如上所示。儘管如此。雖然我在這:我可以發送一個變量在我的app.js中以某種方式發送給我的視圖嗎? – CaptainCarl
是的,你可以。無論您是使用Jade還是EJS作爲您的模板引擎,都可以在兩者中完成。當您在視圖中提及頁面的名稱後使用渲染功能時;你可以傳入你的變量的第二個參數。比方說你有一個變量't',其中你想保留一個字符串作爲頁面的標題,然後函數調用就像這樣'response.render('index',{title:t});''然後您可以在索引頁中使用'title'來獲取var t的字符串值。 –