RangeError:无效的状态码:0

我有一个问题,当我在这个应用程序上尝试一个POST请求时,得到以下错误代码(记住我是一个初学者node.js / js程序员):

错误:

[20:22:28] [nodemon] starting `node app.js` Running server on 3000 Mon, 27 Jun 2016 19:22:31 GMT express deprecated res.send(status, body): Use res.status(status).send(body) instead at routes\edit.js:35:25 c:\Users\Matt\WebstormProjects\ghs_restart\node_modules\mongodb\lib\utils.js:98 process.nextTick(function() { throw err; }); ^ RangeError: Invalid status code: 0 at ServerResponse.writeHead (_http_server.js:192:11) at ServerResponse._implicitHeader (_http_server.js:157:8) at ServerResponse.OutgoingMessage.end (_http_outgoing.js:573:10) at ServerResponse.send (c:\Users\Matt\WebstormProjects\ghs_restart\node_modules\express\lib\response.js:204:10) at ServerResponse.json (c:\Users\Matt\WebstormProjects\ghs_restart\node_modules\express\lib\response.js:249:15) at ServerResponse.send (c:\Users\Matt\WebstormProjects\ghs_restart\node_modules\express\lib\response.js:151:21) at c:\Users\Matt\WebstormProjects\ghs_restart\routes\edit.js:35:25 at c:\Users\Matt\WebstormProjects\ghs_restart\node_modules\mongodb\lib\collection.js:416:18 at handleCallback (c:\Users\Matt\WebstormProjects\ghs_restart\node_modules\mongodb\lib\utils.js:96:12) at c:\Users\Matt\WebstormProjects\ghs_restart\node_modules\mongodb\lib\collection.js:705:5 

app.js:

 var express = require('express'); var router = express.Router(); var app = express(); var bodyParser = require('body-parser'); app.use( bodyParser.json() ); // to support JSON-encoded bodies app.use(bodyParser.urlencoded({ // to support URL-encoded bodies extended: true })); var path = require('path'); var port = process.env.PORT || 3000; var index = require('./routes/index'); var edit = require('./routes/edit'); app.use('/', index); app.use('/edit', edit); app.use(express.static(path.join(__dirname, 'public'))); app.set('view engine', 'jade'); app.set('views', 'views'); app.listen(port, function (err) { console.log("Running server on", port); }); module.exports = index; 

以下是我的edit.js路线,我相信问题正在发生:

 var express = require('express'); var router = express.Router(); var app = express(); var bodyParser = require('body-parser'); app.use( bodyParser.json() ); // to support JSON-encoded bodies app.use(bodyParser.urlencoded({ // to support URL-encoded bodies extended: true })); var path = require('path'); var port = process.env.PORT || 3000; var index = require('./routes/index'); var edit = require('./routes/edit'); app.use('/', index); app.use('/edit', edit); app.use(express.static(path.join(__dirname, 'public'))); app.set('view engine', 'jade'); app.set('views', 'views'); app.listen(port, function (err) { console.log("Running server on", port); }); module.exports = index; 

       

网上收集的解决方案 "RangeError:无效的状态码:0"

我刚才有一个类似的错误信息,并设法通过改变来解决这个问题:

 res.status(statusCode); 

至:

 if (statusCode >= 100 && statusCode < 600) res.status(statusCode); else res.status(500); 

要不就:

 res.status(statusCode >= 100 && statusCode < 600 ? err.code : 500); 

也就是说,确保你没有试图在某个地方设置一个无效的HTTP状态码 。

这可能是这个问题,但它看起来像你不小心复制了app.js代码,而不是粘贴在问题中的edit.js代码。

这种情况也会发生,当我们在表单保存validation错误,我们正在使用res.redirect而不是res.render方法

例如-

请用

 res.render('users/add', { countries: countries }); 

而不是(这是节点的错误陈述)

 res.redirect('/users/add', { countries: countries });