无法使用 ExpressJS 捕获 URL 参数
Can't catch the URL parameters with ExpressJS
我正在尝试从来自 Fitbit API 的回调 URL 中捕获参数值。
回调 URL 如下所示,
http://localhost:9000/callback#access_token=********&user_id=******&scope=睡眠+设置+营养+activity+社交+心率+个人资料+重量+位置&token_type=承载&expires_in=30418415
我在 fitbit API 中通过回调 URL 声明为 http://localhost:9000/callback。
我的 ExpressJS 代码如下。
const express = require('express');
const morgan = require('morgan');
const path = require('path');
const app = express();
app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] :response-time ms'));
app.use(express.static(path.resolve(__dirname, '..', 'build')));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html'));
});
const PORT = process.env.PORT || 9000;
app.get('/callback', function(req, res) {
var access_token = req.param('access_token') || null;
var user_id = req.param('user_id') || null;
res.send(access_token + ' ' + user_id);
});
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}!`);
});
我想不通问题出在哪里。
URL中的#符号是引入framgent标识符。所以你的回调 url http://localhost:3000/callback#access_token=********&user_id=*******&scope=sleep+settings+nutrition+activity+social+heartrate+profile+weight+location&token_type=Bearer&expires_in=30418415
只会得到 http://localhost:3000/callback
而不会向你的服务器发送任何参数。所以,你不能直接在你的服务器中获取这些参数。
但是有解决办法。请参考这个answer.
req.param('token') is depreciated use req.params.token pass the value directly into the url
如果您使用 req.params,请在 url
中指定关键参数
app.get('/callback/:access_token/:user_id', function(req, res) {
//url ==> localhost:9000/callback/1233/123
var access_token = req.params.access_token || null;
var user_id = req.params.user_id || null;
console.log(req.params)
res.send(access_token + ' ' + user_id);
});
if you want the catch the value in the url means use req.query instead of req.params pass the value using the key of req.query
app.get('/callback',function(req, res) {
var access_token = req.query.access_token || null;
var user_id = req.query.user_id || null;
console.log(req.query);
res.send(access_token + ' ' + user_id);
});
我正在尝试从来自 Fitbit API 的回调 URL 中捕获参数值。
回调 URL 如下所示,
http://localhost:9000/callback#access_token=********&user_id=******&scope=睡眠+设置+营养+activity+社交+心率+个人资料+重量+位置&token_type=承载&expires_in=30418415
我在 fitbit API 中通过回调 URL 声明为 http://localhost:9000/callback。
我的 ExpressJS 代码如下。
const express = require('express');
const morgan = require('morgan');
const path = require('path');
const app = express();
app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] :response-time ms'));
app.use(express.static(path.resolve(__dirname, '..', 'build')));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html'));
});
const PORT = process.env.PORT || 9000;
app.get('/callback', function(req, res) {
var access_token = req.param('access_token') || null;
var user_id = req.param('user_id') || null;
res.send(access_token + ' ' + user_id);
});
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}!`);
});
我想不通问题出在哪里。
URL中的#符号是引入framgent标识符。所以你的回调 url http://localhost:3000/callback#access_token=********&user_id=*******&scope=sleep+settings+nutrition+activity+social+heartrate+profile+weight+location&token_type=Bearer&expires_in=30418415
只会得到 http://localhost:3000/callback
而不会向你的服务器发送任何参数。所以,你不能直接在你的服务器中获取这些参数。
但是有解决办法。请参考这个answer.
req.param('token') is depreciated use req.params.token pass the value directly into the url
如果您使用 req.params,请在 url
中指定关键参数 app.get('/callback/:access_token/:user_id', function(req, res) {
//url ==> localhost:9000/callback/1233/123
var access_token = req.params.access_token || null;
var user_id = req.params.user_id || null;
console.log(req.params)
res.send(access_token + ' ' + user_id);
});
if you want the catch the value in the url means use req.query instead of req.params pass the value using the key of req.query
app.get('/callback',function(req, res) {
var access_token = req.query.access_token || null;
var user_id = req.query.user_id || null;
console.log(req.query);
res.send(access_token + ' ' + user_id);
});