[英]Saving the value of req.query['name'] to a variable in Node.jS?
我正在使用node.js编写登录系统,我正在尝试这样做:
var auth = req.query['name']
然后我将auth与数据库查询的结果进行比较,但是每当我尝试使用/?name = foo加载页面时,都会出现此错误:
Error: Can't set headers after they are sent.
我当前的代码是:
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello! every thing is working!');
var auth = req.query['name']
if (auth === "ozzie"){// I will obviously change this.
res.send('Hello! Your details have been authenticated')
}
else
{
res.send('Sorry! That did not work! please try again! ')
}
});
app.listen(8888, '127.0.0.1');
console.log('running on, local Ip + 8888');
问题是您尝试发送两个对同一请求的响应,这是第一次
res.send('hello! every thing is working!');
第二次是在if
or else
块中。 删除第一个,它应该可以正常工作或连接输出,只有一个res.send()
传递给连接的输出。
您正在回调的开头发送响应:
app.get('/', function(req, res){
res.send('hello! every thing is working!'); // here
然后,在有条件的if - else
再次调用同一事物。 由于调用send()
方法设置了标头,因此您最终做了两次相同的操作。
删除第一个,就可以了。
注意:出于调试目的,您可以求助于console.log
并在运行应用程序主文件的控制台上查看调试信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.