[英]node.js & Express / body parser
I write server side application with express
& node.js.
我用
express
& node.js.
编写服务器端应用程序node.js.
I have the next: 我有下一个:
app.configure(function () {
app.use(express.bodyParser());
});
Everything works good, but: 一切正常,但是:
As I understand, this method was deprecated. 据我了解,此方法已被弃用。
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.