bodyParser似乎不起作用 [英] bodyParser doesn't seems to work

查看:552
本文介绍了bodyParser似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,这个问题听起来很简单",但我无法让body-parser来处理这个非常简单的示例:

Sorry if this question may sounds "simple", but I can't get body-parser to work on this very simple example :

"use strict";

const PORT = 3000;
const express = require("express");
const bodyParser = require("body-parser");
const app = express();

app.post("/api/login", (req, res) => {
  if (!req.body) return res.sendStatus(400);
  res.send("welcome, " + req.body.username);
});


app.use(express.json());
app.use(bodyParser.json());

console.log(`Listen on port ${PORT}`);
app.listen(PORT);

尝试使用命令行:

curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login

curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login

它总是以400响应!我尝试了很多配置,但没有找到解决方案.

And it always respond with 400 ! I tried a lot of configuration and didn't find a solution.

我敢肯定这很简单,因为似乎没有人出现相同的错误,所以我错过了一些东西,但不知道是什么!

I'm sure it's pretty simple because no one seems to have the same error, so I'm missing something but don't know what !

表达:4.16.2

body-parser:1.18.2

body-parser : 1.18.2

感谢您的帮助!

编辑:解决的方法是我需要将中间件放置在路径定义之前 之前.就我而言,不需要body-parser,我可以使用内置API的express.jon().

EDIT : The solution was that I needed to put the middleware before the route definition. I my case, body-parser isn't needed I could just use express.jon() built in API.

"use strict";

const PORT = 3000;
const express = require("express");
const app = express();

app.use(express.json());

app.post("/api/login", (req, res) => {
  if (!req.body) return res.sendStatus(400);
  res.send("welcome, " + req.body.username);
});

console.log(`Listen on port ${PORT}`);
app.listen(PORT);

推荐答案

您将在路由定义之后安装bodyparser作为中间件.通常,您要在路由之前定义路由前中间件,或将路由分开到不同的文件.

You're installing bodyparser as middleware after the route definition. Generally you want to define pre-route middlewares before the routes or separate routes out to different files.

只需重新安排代码,如下所示:

Simply re-arrange your code as follows:

'use strict'

const PORT = 3000
const express = require('express')
const bodyParser = require('body-parser')
const app = express()

app.use(bodyParser.json()) // note: this is before the route

app.post('/api/login', (req, res) => {
  console.log(req.body)
  if (!req.body) return res.sendStatus(400)
  res.send('welcome, ' + req.body.username)
})

app.use(express.json())

console.log(`Listen on port ${PORT}`)
app.listen(PORT)

如果您希望在单独的文件中将bodyparser用作中间件,则常见用法如下:

If you wish to use bodyparser in a separate file as a middleware, the common use is as follows:

routes/someroute.js

const bodyParser = require('body-parser')
const jsonParser = bodyParser.json()

module.exports = (app) => {

  app.post('/a/route', jsonParser, (req,res) => {
      ...
  })

}

这篇关于bodyParser似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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