如何从nodejs / express向浏览器发送成功状态? [英] How can I send a success status to browser from nodejs/express?
本文介绍了如何从nodejs / express向浏览器发送成功状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
app.post('/ settings) ',function(req,res){
var myData = {
a:req.param('a')
,b:req.param('b')
, c:req.param('c')
,d:req.param('d')
}
var outputFilename ='config.json';
fs.writeFile(outputFilename,JSON.stringify(myData,null,4),function(err){
if(err){
console.log(err);
} else {
console.log(配置文件已被覆盖);
}
});
});
这允许我获取提交的表单数据并将其写入JSON文件。 >
这样做完美。但客户仍然处于某种状态,最终会超时。所以我需要向客户端发送一些成功状态或成功标题。
我该怎么做?
提前谢谢!
解决方案
Express Update 2015:
res.sendStatus(200)
这已被弃用:
res.send(200)
I've written the following piece of code in my nodeJS/Expressjs server:
app.post('/settings', function(req, res){
var myData = {
a: req.param('a')
,b: req.param('b')
,c: req.param('c')
,d: req.param('d')
}
var outputFilename = 'config.json';
fs.writeFile(outputFilename, JSON.stringify(myData, null, 4), function(err) {
if(err) {
console.log(err);
} else {
console.log("Config file as been overwriten");
}
});
});
This allows me to get the submitted form data and write it to a JSON file.
This works perfectly. But the client remains in some kind of posting state and eventually times out. So I need to send some kind of success state or success header back to the client.
How should I do this?
Thank you in advance!
解决方案
Express Update 2015:
Use this instead:
res.sendStatus(200)
This has been deprecated:
res.send(200)
这篇关于如何从nodejs / express向浏览器发送成功状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文