Javascript 范围错误:无效状态代码:0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38061781/
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
RangeError: Invalid status code: 0
提问by Matt
I am having an issue where I am getting the following error code when attempting a POST request on this application (bearing in mind I am a beginner node.js/js programmer):
我遇到了一个问题,在此应用程序上尝试 POST 请求时收到以下错误代码(请记住,我是一个初级 node.js/js 程序员):
Error:
错误:
[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:
应用程序.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;
The following is my edit.js route, where I believe the issue is occurring:
以下是我的 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;
回答by Bavell
I had a similar error message just now and managed to solve the problem by changing:
我刚才有一个类似的错误消息,并通过更改设法解决了问题:
res.status(statusCode);
to:
到:
if (statusCode >= 100 && statusCode < 600)
res.status(statusCode);
else
res.status(500);
or just:
要不就:
res.status(statusCode >= 100 && statusCode < 600 ? err.code : 500);
I.e. make sure you aren't trying to set an invalid HTTP status codesomewhere.
即确保您没有尝试在某处设置无效的 HTTP 状态代码。
It's likely this is the issue but it looks like you've accidentally duplicated the app.js
code instead of pasting the edit.js
code in the question.
这很可能是问题所在,但看起来您不小心复制了app.js
代码,而不是将edit.js
代码粘贴到问题中。
回答by SohanLal Saini
This case also happen when we have validation error on form save and we are using res.redirect instead of res.render method
当我们在表单保存时出现验证错误并且我们使用 res.redirect 而不是 res.render 方法时也会发生这种情况
for example-
例如-
Please Use
请用
res.render('users/add', {
countries: countries
});
instead of (it's wrong statement for node)
而不是(这是节点的错误陈述)
res.redirect('/users/add', {
countries: countries
});
回答by a_m_dev
for me the issue was that i had not created my upload directory and it returns me the error. may be you should consider to create your ./upload
directory first, because of permission issues on linux.
对我来说,问题是我没有创建我的上传目录,它返回了错误。./upload
由于 linux 上的权限问题,您可能应该考虑先创建目录。