使用参数编写node.js get处理程序?
如何使用node.js为以下URL编写get处理程序使用参数编写node.js get处理程序?,node.js,web-applications,express,Node.js,Web Applications,Express,如何使用node.js为以下URL编写get处理程序 http://localhost:3000/auth?code=xxxxxxx 以下代码不起作用 app.get('/auth', function (req,res) { }); 它不起作用,因为它什么都不做。您需要发送响应: app.get('/auth', function (req,res) { res.send('Hi it worked. Code: ' + req.query.code); }); 另一种方法是:
http://localhost:3000/auth?code=xxxxxxx
以下代码不起作用
app.get('/auth', function (req,res) {
});
它不起作用,因为它什么都不做。您需要发送响应:
app.get('/auth', function (req,res) {
res.send('Hi it worked. Code: ' + req.query.code);
});
另一种方法是:
app.get('/auth/:code', function (req,res) {
res.send('Hi it worked. Code: ' + req.params.code);
});
而URL就是
http://localhost:3000/auth/xxxxxxx
它不工作,因为它什么都不做。您需要发送响应:
app.get('/auth', function (req,res) {
res.send('Hi it worked. Code: ' + req.query.code);
});
另一种方法是:
app.get('/auth/:code', function (req,res) {
res.send('Hi it worked. Code: ' + req.params.code);
});
而URL就是
http://localhost:3000/auth/xxxxxxx
它不工作,因为它什么都不做。您需要发送响应:
app.get('/auth', function (req,res) {
res.send('Hi it worked. Code: ' + req.query.code);
});
另一种方法是:
app.get('/auth/:code', function (req,res) {
res.send('Hi it worked. Code: ' + req.params.code);
});
而URL就是
http://localhost:3000/auth/xxxxxxx
它不工作,因为它什么都不做。您需要发送响应:
app.get('/auth', function (req,res) {
res.send('Hi it worked. Code: ' + req.query.code);
});
另一种方法是:
app.get('/auth/:code', function (req,res) {
res.send('Hi it worked. Code: ' + req.params.code);
});
而URL就是
http://localhost:3000/auth/xxxxxxx
请注意,一些客户端应该接受特定的响应类型。
例如,您应该发送一个JSON对象作为响应
因此,与其只响应字符串,不如将JSON对象发送为:
app.get('/auth', function (req,res) {
res.send({ 'response' : 'Hi it worked.', 'code': req.query.code });
});
请注意,一些客户端应该接受某种响应类型。 例如,您应该发送一个JSON对象作为响应 因此,与其只响应字符串,不如将JSON对象发送为:
app.get('/auth', function (req,res) {
res.send({ 'response' : 'Hi it worked.', 'code': req.query.code });
});
请注意,一些客户端应该接受某种响应类型。 例如,您应该发送一个JSON对象作为响应 因此,与其只响应字符串,不如将JSON对象发送为:
app.get('/auth', function (req,res) {
res.send({ 'response' : 'Hi it worked.', 'code': req.query.code });
});
请注意,一些客户端应该接受某种响应类型。 例如,您应该发送一个JSON对象作为响应 因此,与其只响应字符串,不如将JSON对象发送为:
app.get('/auth', function (req,res) {
res.send({ 'response' : 'Hi it worked.', 'code': req.query.code });
});