简体   繁体   English

node.js&Express /正文解析器

[英]node.js & Express / body parser

I write server side application with express & node.js. 我用expressnode.js.编写服务器端应用程序node.js.

I have the next: 我有下一个:

app.configure(function () {
    app.use(express.bodyParser());
});

Everything works good, but: 一切正常,但是:

  1. As I understand, this method was deprecated. 据我了解,此方法已被弃用。

  2. The next method doesn't work well. 下一种方法效果不佳。 It writes some random chars instead of writing the correct chars: 它编写一些随机字符,而不是编写正确的字符:

     app.post('/randomWrite', function (req, res) { var fileName = req.body.name; var contentLength = parseInt(req.files.file._writeStream.bytesWritten); var start = parseInt(req.body.chunk) * 102400; var buffer = new Buffer(parseInt(req.files.file._writeStream.bytesWritten)); fs.open(req.files.file.path, 'r', function (status, fd) { if (fd == null) { console.log("Can't open the file with the fd"); return; } fileNameLocation = "./" + fileName; fs.open(fileNameLocation, 'w+', function (err, fd1) { fs.read(fd, buffer, 0, contentLength, start, function (err, bytesRead, buffer1) { if (err) console.log("ERROR: " + err); fs.write(fd1, buffer1, 0, contentLength, start, function (err, bytesWrite, buffer) { if (req.body.chunk == req.body.chunks - 1) { fs.close(fd, function (err) { }) fs.close(fd1, function (err) { }) FileServer.prototype.returnResCodeWithId(res, 200, id); } else { fs.close(fd, function (err) { }) fs.close(fd1, function (err) { }) FileServer.prototype.returnResCode(res, 200); } }) }) }) }) 

Instead of writing in the correct offset, it seems that something get wrong and some text from the middleware (bodyParser) is written. 似乎没有写正确的偏移量,而是出了点问题,并写了中间件(bodyParser)中的某些文本。

How can i change the express.bodyParser()? 如何更改express.bodyParser()? It will fix my problem with the writing? 它将解决我的写作问题吗?

You need to use the Body-parser middleware. 您需要使用Body-parser中间件。

You can install it with 您可以使用

npm install body-parser

and include it with Express using 并使用Express将其包括在内

var bodyparser = require('body-parser');
app.use(bodyparser());

If you want file uploads too, then have a look at multer . 如果您也要上传文件,请查看multer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 node.js,express和主体解析器中的request.body为空 - request.body is empty in node.js, express and body parser Node.js/Express.js 请求正文未定义。 body-parser 已安装并导入 - Node.js/Express.js request body is undefined. body-parser is installed and imported 未使用express 4.16.X,body-Parser 1.X定义的node.js req.body - node.js req.body undefined with express 4.16.X, body-Parser 1.X Node.JS正文解析器问题 - Node.JS body parser issue ECMAScript 6对node.js及其不同程序包的支持,例如本机表达body_parser - ECMAScript 6 support for node.js and its different packages like express body_parser natively 如何通过 Body-parser 获取 post 值 - Express Node.js - How to get post values by Body-parser - Express Node.js Node.js:Request.body未定义(使用body-parser) - Node.js: Request.body undefined (with body-parser) 通过 AJAX (Vanilla JavaScript) 的 POST 请求在 Node.js (express/body-parser/ejs) 上生成空对象 - POST request via AJAX (Vanilla JavaScript) produce empty object on Node.js (express/body-parser/ejs) 无法使用node.js v0.12.7检索POST数据,表示为4,正文解析器,引导程序3 - Can't retrieve POST data using node.js v0.12.7, express 4, body-parser, bootstrap 3 fetch:从主体解析器获取未定义的数据(Express,节点js) - fetch:undefined data getting from body parser (Express,node js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM