gpt4 book ai didi

Node.js express : confuse about router middleware

转载 作者:太空宇宙 更新时间:2023-11-03 22:03:01 24 4
gpt4 key购买 nike

没有路由器中间件,服务器端一旦收到post数据,就可以正常记录:

var express = require('express');
var app = express.createServer();
app.configure(function () {
app.use(express.static(__dirname + '/static'));
// app.use(app.router);
app.use(express.bodyParser());
})

app.get('/', function(req, res){
res.send('Hello World');
});

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

app.listen(8000);

但是一旦我使用router中间件,日志结果是undefined并且没有任何响应

为什么?我该如何解决这个问题?

最佳答案

问题出在你调用router的顺序

bodyParser 必须在 router 之前调用

app.configure(function () {
app.use(express.static(__dirname + '/static'));
app.use(express.bodyParser());
app.use(app.router);
});

关于Node.js express : confuse about router middleware,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11685750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com