2012-02-19 86 views
4

我尝试了很多财产 '标题' 弄清楚为什么这引发了这样的错误:Expressjs POST错误:类型错误:无法读取的不确定

Configuring 
Listening on 2000 
TypeError: Cannot read property 'title' of undefined 
    at /home/abdulsattar/learning/node/express/index.js:9:20 

的index.js:

var express = require("express"), 
    app = express.createServer(); 

app.get("/", function(req, res) { 
    res.send('<form action="/new" method="post"><input type="text" name="title" /><input type="submit" /></form>'); 
}); 

app.post("/new", function(req, res) { 
    res.send(req.body.title); 
}); 

app.configure(function() { 
    console.log("Configuring"); 
    app.use(express.bodyParser()); 
}); 

var port = process.env.PORT || 2000; 

app.listen(port, function() { 
    console.log("Listening on " + port); 
}); 

我已经读过快递需要bodyParser()。我在use以上,但它总是失败。我在版本2.5.82.5.8(认为可能是问题)上尝试过,但两个版本均失败。有什么我失踪?

回答

11

我的预感,尝试移动你的app.configure语句之前你的app.get和app.post。 bodyParser中间件没有被调用。此外,为了安全起见,将enctype添加到表单中,不应该有必要,但不管:application/x-www-form-urlencoded

让我知道...

+1

哦,伙计!就是这样!我在处理程序之前移动了configure语句,并且它工作正常。非常感谢! – 2012-02-19 10:58:59

相关问题