Node.js(带有 express 和 bodyParser):无法从 post 请求中获取表单数据

编程入门 行业动态 更新时间:2024-10-27 04:38:18
本文介绍了Node.js(带有 express 和 bodyParser):无法从 post 请求中获取表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我似乎无法恢复发送到我的 Node.js 服务器的 post 请求的表单数据.我已经把服务器代码和发布请求(在 chrome 中使用邮递员发送)放在下面:

I can't seem to recover the form-data of a post request sent to my Node.js server. I've put below the server code and the post request (sent using postman in chrome):

发布请求

POST /api/login HTTP/1.1 Host: localhost:8080 Cache-Control: no-cache ----WebKitFormBoundaryE19zNvXGzXaLvS5C Content-Disposition: form-data; name="userName" jem ----WebKitFormBoundaryE19zNvXGzXaLvS5C

NodeJS 服务器代码

var express = require('express'); // call express var app = express(); // define our app using express var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(bodyParser()); app.all('/*', function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Headers', 'Content-Type,accept,access_token,X-Requested-With'); next(); }); var port = process.env.PORT || 8080; // set our port var router = express.Router(); // get an instance of the express Router router.get('/', function(req, res) { res.json({ message: 'I am groot!' }); }); // Login router.route('/login') .post(function(req, res){ console.log('Auth request recieved'); // Get the user name var user = req.body.userName; var aToken = getToken(user); res.json({ 'token':'a_token' }); }); app.use('/api', router); app.listen(port);

登录方法尝试获取req.body.userName,但是,req.body 始终为空.我在 SO 上看到其他案例描述了这种行为,但没有一个相关的答案在这里适用.

The login method tries to obtain the req.body.userName, however, req.body is always empty. I've seen other cases on SO describing such behavior but none of the related answers did apply here.

感谢您的帮助.

推荐答案

一般来说,一个 express 应用需要指定合适的body-解析器中间件,以便 req.body 包含正文.

In general, an express app needs to specify the appropriate body-parser middleware in order for req.body to contain the body.

  • 如果您需要解析 url 编码(非多部分)表单数据以及 JSON,请尝试添加:

  • If you required parsing of url-encoded (non-multipart) form data, as well as JSON, try adding: // Put this statement near the top of your module var bodyParser = require('body-parser'); // Put these statements before you define any routes. app.use(bodyParser.urlencoded()); app.use(bodyParser.json());

    首先,您需要将 body-parser 添加到 dependenciespackage.json 的属性,然后执行 npm update.

    First, you'll need to add body-parser to the dependencies property of your package.json, and then perform a npm update.

    要处理多部分表单数据,bodyParser.urlencoded() 正文解析器将不起作用.请参阅此处推荐的模块以解析多部分主体.

    To handle multi-part form data, the bodyParser.urlencoded() body parser will not work. See the suggested modules here for parsing multipart bodies.

  • 更多推荐

    Node.js(带有 express 和 bodyParser):无法从 post 请求中获取表单数据

    本文发布于:2023-10-11 04:41:21,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1480710.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:表单   数据   js   Node   express

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!