快速js身体解析器不工作? [英] Express js body parser not working?

查看:118
本文介绍了快速js身体解析器不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var app = express,我的节点服务器中使用了express(截断到重要部分) createServer(); 

app.all(/ test /,function(req,res){
console.log(req.headers);
console.log(req.body);
res.send();
});

function appStart(cobrands){
app.configure(function(){
app.use(express.bodyParser());
app.use(express .cookieParser());

app.use('/ min',express.static('../min'));
app.use('/ js',express 。。('../ js'));
app.use('/ css',express.static('../ css'));
app.use('/ img' ,express.static('../ img'));
});
app.listen(8080);
}

然后我有一个简单的表单调用localhost:8080像这样:

 < form action =http:// localhost:8080 / testmethod =post> 
< input type =hiddenname =testvalue =testing/>
< input type =submitname =submitvalue =to node/>
< / form>

但express.bodyParser似乎没有做任何事情,并且req.body是未定义的。以下是 console.log的输出 s:

  // req .headers 
{host:'localhost:8080',
'user-agent':'Mozilla / 5.0(Macintosh; Intel Mac OS X 10_7_1)AppleWebKit / 534.48.3(KHTML,像Gecko)版本/5.1 Safari / 534.48.3',
'content-length':'27',
accept:'text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8',
origin:'file://',
'content-type':'application / x-www-form-urlencoded',
'accept-language ':'en-us',
'accept-encoding':'gzip,deflate',
cookie:'',
connection:'keep-alive'}
/ / req.body
undefined

注意: content-type 正确定义为 application / x-www-form-urlencoded ,因为它应该是bodyParser的工作,我已经验证了它已经过去了通过在Safari中弹出打开调试工具,并验证表单数据是否存在>

解决方案

http://expressjs.com/ guide.html#configuration


请注意使用app.router,可以(可选)用于挂载
应用程序路由,否则首次调用app.get(),
app.post()等将挂载路由。


在添加任何其他中间件之前,您正在调用app.all()。这样做有效地将app.router放在所有其他中间件的前面,导致它们永远不会用于在路由中结束的请求。



安装应用程序路由是与执行 app.use(app.router); 相同。



最后你的堆栈是看看这个:

  app.use(app.router); //包含/ test / route 
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use('/ min',express.static('../ min'));
app.use('/ js',express.static('../ js'));
app.use('/ css',express.static('../ css'));
app.use('/ img',express.static('../ img'));

tl; dr将您的路由在您的app.configure()和app.listen() 。


I have the following in my node server using express (truncated to the important parts):

var app = express.createServer();

app.all(/test/,function(req,res){
    console.log(req.headers);
    console.log(req.body);
    res.send("");
});

function appStart(cobrands){
    app.configure(function(){
        app.use(express.bodyParser());
        app.use(express.cookieParser());

        app.use('/min',express.static('../min'));
        app.use('/js',express.static('../js'));
        app.use('/css',express.static('../css'));
        app.use('/img',express.static('../img'));
    });
    app.listen(8080);
}

I then have a simple form that calls out to localhost:8080 like so:

<form action="http://localhost:8080/test" method="post">
    <input type="hidden" name="test" value="testing"/>
    <input type="submit" name="submit" value="to node"/>
</form>

But express.bodyParser doesn't seem to be doing anything, and req.body is undefined. Here's the output of the console.logs:

// req.headers
{ host: 'localhost:8080',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/534.48.3 (KHTML, like Gecko) Version/5.1 Safari/534.48.3',
  'content-length': '27',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  origin: 'file://',
  'content-type': 'application/x-www-form-urlencoded',
  'accept-language': 'en-us',
  'accept-encoding': 'gzip, deflate',
  cookie: '',
  connection: 'keep-alive' }
// req.body
undefined

Note: content-type is correctly defined as application/x-www-form-urlencoded as it should be for bodyParser to work, and I've verified that it's coming over by popping open the debug tools in Safari and verifying that the form data is present.

解决方案

http://expressjs.com/guide.html#configuration

Note the use of app.router, which can (optionally) be used to mount the application routes, otherwise the first call to app.get(), app.post(), etc will mount the routes.

What's happening is you're calling app.all() before adding any of the other middleware. Doing this effectively puts app.router in front of all of your other middleware, resulting in them never being used on requests that end inside your routes.

Mounting the application routes is pretty much the same as doing app.use(app.router);.

In the end your stack is looks this:

app.use(app.router); // Contains your /test/ route
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use('/min',express.static('../min'));
app.use('/js',express.static('../js'));
app.use('/css',express.static('../css'));
app.use('/img',express.static('../img'));

tl;dr Move your route between your call to app.configure() and app.listen().

这篇关于快速js身体解析器不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆