使用POST数据写入本地文件与node.js和express [英] Using POST data to write to local file with node.js and express
本文介绍了使用POST数据写入本地文件与node.js和express的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试处理简单的POST请求并将数据附加到本地文件。但是,当我尝试使用邮递员发送原始文本时a>,例如hi world,实际附加的是 [object Object]
。我不知道如果没有任何东西应该解释为任一端的对象,可能会导致什么。谢谢!
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!
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);
推荐答案
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);
这篇关于使用POST数据写入本地文件与node.js和express的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文