node.js&快速/身体解析器

时间:2014-06-17 09:34:33

标签: javascript node.js express

我用express&编写服务器端应用程序node.js.

我有下一个:

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

一切都很好,但是:

  1. 据我了解,此方法已弃用。

  2. 下一个方法效果不佳。它写了一些随机字符而不是写正确的字符:

    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);
                    }
                })
            })
        })
    })
    
  3. 而不是写入正确的偏移量,似乎出现了问题,并且编写了中间件(bodyParser)中的一些文本。

    如何更改express.bodyParser()?它会解决我的写作问题吗?

1 个答案:

答案 0 :(得分:0)

您需要使用Body-parser中间件。

您可以使用

进行安装
npm install body-parser

并使用Express

将其包含在内
var bodyparser = require('body-parser');
app.use(bodyparser());

如果您还想上传文件,请查看multer

相关问题