我坚持这个问题,看看是否有人可以提供帮助。 我有这个Node.js应用程序,当我提交表单并转到注册部分时,我收到此错误:
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:574:10)
at ServerResponse.send (C:\projects\authentication\node_modules\express\lib\response.js:211:10)
at ServerResponse.json (C:\projects\authentication\node_modules\express\lib\response.js:256:15)
at C:\projects\authentication\app.js:24:7
at Layer.handle_error (C:\projects\authentication\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (C:\projects\authentication\node_modules\express\lib\router\index.js:315:13)
at C:\projects\authentication\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\projects\authentication\node_modules\express\lib\router\index.js:335:12)
这是app.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// include routes
var routes = require('./routes/router');
app.use('/', routes);
app.use('/register', routes);
// serve static files from /public
app.use(express.static(__dirname + '/template'));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('File Not Found');
err.status = 404;
next(err);
});
// error handler
// define as the last app.use callback
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json('error', {
message: err.message,
error: {}
});
});
/////// JSON parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(function (req, res) {
res.setHeader('Content-Type', 'text/plain');
res.write('you posted:\n');
res.end(JSON.stringify(req.body, null, 2));
});
// listen on port 3000 setting server
app.listen(3000, function () {
console.log('Express app listening on port 3000');
});
to handle the http request i have a route.js file, wich is like this:
var express = require('express');
var router = express.Router();
var path = require('path');
// GET /
router.get('/', function(req, res, next) {
//res.setHeader('Content-type','text/plain');
return res.sendFile(path.join(__dirname + "/../index.html"));
});
router.post('/register', function(req, res){
//console.log(req.body);
return res.send(req.body.email);
/*
if (req.body.email &&
req.body.username &&
req.body.password &&
req.body.passwordConf) {
var userData = {
email: req.body.email,
username: req.body.username,
password: req.body.password,
passwordConf: req.body.passwordConf
};
//use schema.create to insert data into the db
User.create(userData, function (err, user) {
if (err) {
return next(err);
} else {
return res.redirect('/profile');
}
});
}*/
});
module.exports = router;
当我尝试通过根'/'上的get请求访问它时打开带有表单的html,这个表单转到'/ register'然后我得到错误,我有点奇怪我怎么得到html文件罚款通过root,但当我尝试访问'/ register'我只得到这个错误,已经尝试了很多东西,我不能做我做错了什么,希望我能得到一些帮助。 感谢。
答案 0 :(得分:3)
看看堆栈跟踪:
RangeError: Invalid status code: 0
at ServerResponse.writeHead (_http_server.js:192:11)
...
at C:\projects\authentication\app.js:24:7
好的,所以错误是由app.js
的第24行引起的,就是这个错误:
res.json('error', {
message: err.message,
error: {}
});
Express试图将'error'
解释为状态代码(因为旧版本的Express接受res.json(status, obj)
已被弃用),并且因为它不是看起来像数字的东西,所以内部会得到转换为数字“0”,这是一个无效的状态代码,因此错误。
我猜你的意思是:
res.json({
message: err.message,
error: {}
});
编辑:至于您的其他错误,您需要确保在路线之前宣布body-parser
中间件为:
var bodyParser = require('body-parser');
/////// JSON parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// include routes
var routes = require('./routes/router');
app.use('/', routes);
app.use('/register', routes);
否则,请求在到达路由处理程序之前不会通过该中间件传递。
答案 1 :(得分:0)
这是HTML文件
</head>
<body>
<form action="/register" method="post">
<div>
<label for="email">email:</label>
<input type="email" name="email" id="email" />
</div>
<div>
<label for="username">username:</label>
<input type="text" name="username" id="username" />
</div>
<div>
<label for="password">password:</label>
<textarea id="password" name="password"></textarea>
</div>
<div>
<label for="passwordConf">password again:</label>
<textarea name="passwordConf" id="passwordConf"></textarea>
</div>
<div class="button">
<button type="submit">submit</button>
</div>
</form>
</body>
</html>