使用 POST 数据通过 node.js 和 express 写入本地文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17981677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:09:59  来源:igfitidea点击:

Using POST data to write to local file with node.js and express

node.jspostexpress

提问by user4815162342

I'm trying to just handle simple POST requests and append the data to a local file. However, when I try to POST raw text with postman, such as 'hi world', what's actually appended is [object Object]. I'm not sure what could be causing this if nothing should be interpreted as an object on either end. Thank you!

我试图只处理简单的 POST 请求并将数据附加到本地文件。但是,当我尝试使用postmanPOST 原始文本时,例如 'hi world',实际附加的是[object Object]. 如果在任何一端都不应将任何内容解释为对象,我不确定是什么导致了这种情况。谢谢!

var express = require('express'),
    fs = require('fs')
    url = require('url');
var app = express();

app.configure(function(){
  app.use('/public', express.static(__dirname + '/public'));  
  app.use(express.static(__dirname + '/public')); 
  app.use(express.bodyParser());
});

app.post('/receive', function(request, respond) {
    filePath = __dirname + '/public/data.txt';
    fs.appendFile(filePath, request.body, function () {
        respond.end();
    });
});

app.listen(8080);

回答by user4815162342

var express = require('express'),
    fs = require('fs')
    url = require('url');
var app = express();

app.use('/public', express.static(__dirname + '/public'));  
app.use(express.static(__dirname + '/public')); 

app.post('/receive', function(request, respond) {
    var body = '';
    filePath = __dirname + '/public/data.txt';
    request.on('data', function(data) {
        body += data;
    });

    request.on('end', function (){
        fs.appendFile(filePath, body, function() {
            respond.end();
        });
    });
});

app.listen(8080);

回答by Peter Lyons

If you want to do POST requests with regular urlencoded bodies, you don't want to use bodyParser(since you don't actually want to parse the body, you just want to stream it to the filesystem). Consider just streaming the chunks of data with req.pipe(writeableStreamToYourFile).

如果您想使用常规 urlencoded 正文执行 POST 请求,则不想使用bodyParser(因为您实际上并不想解析正文,您只想将其流式传输到文件系统)。考虑只使用req.pipe(writeableStreamToYourFile).

If you want to do file uploads, you can use bodyParserfor that, but it handles multiple files and writes them to disk for you and you would need to iterate through req.filesand copy them from a temporary directory to your target file.

如果你想上传文件,你可以使用bodyParser它,但它会处理多个文件并将它们写入磁盘,你需要遍历req.files它们并将它们从临时目录复制到目标文件。

回答by spacedev

If you want to store Json data then file should be of **.Json type. Otherwise try casting it into string and write into **.txt file. Like

如果要存储 Json 数据,则文件应为 **.Json 类型。否则尝试将其转换为字符串并写入 **.txt 文件。喜欢

var fs = require('fs');
var writer = fs.createWriteStream('output.txt');
response = {
name: '',
id: ''
}
writer.write(JSON.stringify(response));