如何从nodejs / express向浏览器发送成功状态?

我已经在nodeJS / Expressjs服务器中编写了以下代码片段: 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"); } }); }); 这使我可以获取提交的表单数据并将其写入JSON文件。 这完美的作品。 但客户仍处于某种发布状态,最终超时。 所以我需要发送某种成功的状态或成功标题回到客户端。 我应该怎么做? 先谢谢你!

赶上所有路线除了/login

我目前正在编写一个API,这将要求用户在每个请求的标题中传递一个身份validation令牌。 现在我知道我可以创build一个catchall路线说 app.get('/*', function(req,res){ }); 但我想知道如何使它不包括某些路线,如/login或/ ?

与Mongoose进行多对多的映射

我的devise中有FlashcardSchemas和PackageSchemas。 一个flashcard可以属于不同的包,一个包可以包含不同的flashcard。 下面你可以看到我的mongoose模式定义的精简版本: // package-schema.js var Schema = mongoose.Schema, ObjectId = Schema.ObjectId; var PackageSchema = new Schema({ id : ObjectId, title : { type: String, required: true }, flashcards : [ FlashcardSchema ] }); var exports = module.exports = mongoose.model('Package', PackageSchema); // flashcard-schema.js var Schema = mongoose.Schema, ObjectId = Schema.ObjectId; var FlashcardSchema = new Schema({ […]

Node.js + Express:应用程序不会开始侦听端口80

我创build并启动一个像这样的应用程序: express -s -t ejs npm install express npm install ejs node app.js 它工作(在端口3000)。 但是,当我去端口更改为80,然后运行node app.js输出: node.js:198 throw e; // process.nextTick error, or 'error' event on first tick ^ TypeError: Cannot call method 'getsockname' of null at HTTPServer.address (net.js:746:23) at Object.<anonymous> (/var/www/thorous/app.js:35:67) at Module._compile (module.js:432:26) at Object..js (module.js:450:10) at Module.load (module.js:351:31) at Function._load (module.js:310:12) at […]

node.js是否支持“let”语句?

node.js是否支持类似于MDN上描述的let语句? ? var x = 8, y = 12; let ( x = 5, y = 10) { return x + y; } //15 如果没有,有没有办法用自动执行的匿名函数或其他东西来复制function? 和/或有另一个JS环境 已经let和和 有节点的REPL吗? 犀牛? 编辑 : 这个问题在很久以前就被问到了。 截至目前,2015年底,答案是“是的,是的”。 在io.js 3.3中默认包含了和谐特性,并且最近被4.x版本带回到了node.js。

'快车'不被识别的命令(窗口)

好的,我在Windows上运行节点(7)。 使用npm我只是安装模块到d:\目录。 因此,我的文件结构如下所示: D:\ -myproject -node_modules -.bin -express 但是,当我在这个“myproject”目录中,我似乎无法运行“express”,例如: D:\myproject\express site 'express' is not recognized as an internal or external command, operable program or batch file. 我做错了什么?

当缓冲使用streampipe道时,节点回应服务器的性能下降10倍

在节点v8.1.4和v6.11.1上 我从下面的回声服务器实现开始,我将其称为pipe.js或pipe 。 const http = require('http'); const handler = (req, res) => req.pipe(res); http.createServer(handler).listen(3001); 我用wrk和下面的lua脚本(简写为简称)进行了基准testing,将发送一个小的身体作为有效载荷。 wrk.method = "POST" wrk.body = string.rep("a", 10) 在每秒2k个请求和平均延迟为44ms的情况下,性能不是很好。 所以我写了另一个使用中间缓冲区的实现,直到请求完成,然后写出这些缓冲区。 我将这个称为buffer.js或缓冲区 。 const http = require('http'); const handler = (req, res) => { let buffs = []; req.on('data', (chunk) => { buffs.push(chunk); }); req.on('end', () => { res.write(Buffer.concat(buffs)); res.end(); }); […]

Node.js WebRTC客户端

我正在寻找node.js的webrtc实现 从一个nodeJS客户端向另一个webRTC对等端传输数据。 所以在我的情况下,nodejs应用程序不是服务器,而是客户端。 这样的节点模块是否存在?

expressjs日志的最佳做法是什么?

我正在build立一个基于expressjs的应用程序,我想logging下所有的事件。 我可以find温斯顿,这似乎很酷。 无论如何,我正在寻找一种方式来连接到我的expressjs应用程序。 我也想要的是在应用程序内部进行日志logging。 我的reqeusts不是那么简单,所以我想logging我的应用程序内的一切(不仅要求)。 我目前的情况: server.js (我想在这个级别上logginghttp请求) var express = require('express'); var app = express(); var fs = require('fs'); // Post parser app.configure(function(){ app.use(express.bodyParser()); }); // Load routes require('fs').readdirSync(__dirname + '/routes').forEach(function(file) { require(__dirname + '/routes/' + file)(app); }); // 404: Not found app.use(function(req, res, next){ res.json(404, {ERROR: 'Page not found.'}); }); // 500: Error […]

Javascript中的括号括起来

Javascript是什么让我可以在这样的函数调用中使用倒置/倒退括号? 我正在CLI的Node控制台上运行; 特别是节点版本0.10.25。 function a(){ return 42 } a() // -> 42 a)( // -> 42. WTF? function b(t){ return t } b(4) // -> 4 b)4( // No function evaluation; presumably dangling parentheses b)(4 // -> 4. WTF? 附录:这在Chrome 33.0.1750.152,Safari 7.0.2或Firefox 27.01中似乎不起作用。 这实际上是某种ECMAScript解释的“特征”,还是一个节点的特性? 如果节点使用V8,不应该与Chrome结果相匹配吗?