Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

javascript - RangeError: Invalid status code: 0

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):

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 routesedit.js:35:25
c:UsersMattWebstormProjectsghs_restart
ode_modulesmongodblibutils.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:UsersMattWebstormProjectsghs_restart
ode_modulesexpresslib
esponse.js:204:10)
    at ServerResponse.json (c:UsersMattWebstormProjectsghs_restart
ode_modulesexpresslib
esponse.js:249:15)
    at ServerResponse.send (c:UsersMattWebstormProjectsghs_restart
ode_modulesexpresslib
esponse.js:151:21)
    at c:UsersMattWebstormProjectsghs_restart
outesedit.js:35:25
    at c:UsersMattWebstormProjectsghs_restart
ode_modulesmongodblibcollection.js:416:18
    at handleCallback (c:UsersMattWebstormProjectsghs_restart
ode_modulesmongodblibutils.js:96:12)
    at c:UsersMattWebstormProjectsghs_restart
ode_modulesmongodblibcollection.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;

The following is my edit.js route, where I believe the issue is occurring:

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 code somewhere.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...