Express.js;在全球范围内使用路由变量
Express.js; use route variables on a global scale
我正在尝试将 Spotify API 与我的 express.js 服务器一起使用,并且需要从 URL 参数访问授权码。我在 spotify 仪表板中指定的回调路由中使用 let code = req.query.code
提取了这个值。然后我需要将其传递到 POST 请求中(我正在使用请求模块,因为 api 的文档使用 cURL 示例)。但是,我不能全局使用 code
变量,并希望避免使用全局变量。我该如何实现?
重申一下,我想使用这条路线的代码变量:
app.get("/auth/spotify/callback", (req, res) => {
let code = req.query.code;
res.send({
"code": code
})
})
在此选项中JSON:
var options = {
'method': 'POST',
'url': 'https://accounts.spotify.com/api/token',
'headers': {
'Authorization': 'Basic MmV...WU=',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': 'http://localhost:5000/auth/spotify/callback'
}
};
然后在此请求调用中使用:
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
每当我 运行 服务器时,它崩溃并指向选项 JSON 中使用的代码实例,声称它未定义。这可能是一个简单的 JavaScript 问题,但我仍在学习后端实现。
这里是错误:
C:\Users\Finn\Documents\Web\Projects\spotify application\server.js:30
'code': code,
^
ReferenceError: code is not defined
at Object.<anonymous> (C:\Users\Finn\Documents\Web\Projects\spotify application\server.js:30:15)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
server.js:
// ===server.js===
// MODULES
const request = require("request")
const express = require("express");
const app = new express();
// Auth vars
const [clientID, clientSecret, callbackURL] = ["2e...35", "5b...ae", "http%3A%2F%2Flocalhost%3A5000%2Fauth%2Fspotify%2Fcallback"]
// redirects to spotify to get code
app.get("/auth/spotify", (req, res) => {
// takes to auth and then redirects to localhost:5000/auth/spotify/callback
res.redirect(`https://accounts.spotify.com/authorize?client_id=${clientID}&response_type=code&redirect_uri=${callbackURL}&scope=user-top-read`)
})
// saves code in variable
app.get("/auth/spotify/callback", (req, res) => {
let code = req.query.code;
res.send({ "code": code })
})
var options = {
'method': 'POST',
'url': 'https://accounts.spotify.com/api/token',
'headers': {
'Authorization': 'Basic Basic MmV...WU=',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': 'http://localhost:5000/auth/spotify/callback'
}
};
// performs post request to receive access_token and refresh_token
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
// sets port variable
const PORT = process.env.PORT || 5000;
// start server function
app.listen(PORT, () => {
console.log(`app is listening on port ${PORT}`);
});
感谢您的帮助。
您可以使用 code
作为参数调用 request
的函数:
function whateverName(code) {
var options = {
'method': 'POST',
'url': 'https://accounts.spotify.com/api/token',
'headers': {
'Authorization': 'Basic Basic MmV...WU=',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': 'http://localhost:5000/auth/spotify/callback'
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
}
app.get("/auth/spotify/callback", (req, res) => {
let code = req.query.code;
whateverName(code); // Sent!
res.send({
"code": code
});
})
我正在尝试将 Spotify API 与我的 express.js 服务器一起使用,并且需要从 URL 参数访问授权码。我在 spotify 仪表板中指定的回调路由中使用 let code = req.query.code
提取了这个值。然后我需要将其传递到 POST 请求中(我正在使用请求模块,因为 api 的文档使用 cURL 示例)。但是,我不能全局使用 code
变量,并希望避免使用全局变量。我该如何实现?
重申一下,我想使用这条路线的代码变量:
app.get("/auth/spotify/callback", (req, res) => {
let code = req.query.code;
res.send({
"code": code
})
})
在此选项中JSON:
var options = {
'method': 'POST',
'url': 'https://accounts.spotify.com/api/token',
'headers': {
'Authorization': 'Basic MmV...WU=',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': 'http://localhost:5000/auth/spotify/callback'
}
};
然后在此请求调用中使用:
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
每当我 运行 服务器时,它崩溃并指向选项 JSON 中使用的代码实例,声称它未定义。这可能是一个简单的 JavaScript 问题,但我仍在学习后端实现。
这里是错误:
C:\Users\Finn\Documents\Web\Projects\spotify application\server.js:30
'code': code,
^
ReferenceError: code is not defined
at Object.<anonymous> (C:\Users\Finn\Documents\Web\Projects\spotify application\server.js:30:15)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
server.js:
// ===server.js===
// MODULES
const request = require("request")
const express = require("express");
const app = new express();
// Auth vars
const [clientID, clientSecret, callbackURL] = ["2e...35", "5b...ae", "http%3A%2F%2Flocalhost%3A5000%2Fauth%2Fspotify%2Fcallback"]
// redirects to spotify to get code
app.get("/auth/spotify", (req, res) => {
// takes to auth and then redirects to localhost:5000/auth/spotify/callback
res.redirect(`https://accounts.spotify.com/authorize?client_id=${clientID}&response_type=code&redirect_uri=${callbackURL}&scope=user-top-read`)
})
// saves code in variable
app.get("/auth/spotify/callback", (req, res) => {
let code = req.query.code;
res.send({ "code": code })
})
var options = {
'method': 'POST',
'url': 'https://accounts.spotify.com/api/token',
'headers': {
'Authorization': 'Basic Basic MmV...WU=',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': 'http://localhost:5000/auth/spotify/callback'
}
};
// performs post request to receive access_token and refresh_token
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
// sets port variable
const PORT = process.env.PORT || 5000;
// start server function
app.listen(PORT, () => {
console.log(`app is listening on port ${PORT}`);
});
感谢您的帮助。
您可以使用 code
作为参数调用 request
的函数:
function whateverName(code) {
var options = {
'method': 'POST',
'url': 'https://accounts.spotify.com/api/token',
'headers': {
'Authorization': 'Basic Basic MmV...WU=',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': 'http://localhost:5000/auth/spotify/callback'
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
}
app.get("/auth/spotify/callback", (req, res) => {
let code = req.query.code;
whateverName(code); // Sent!
res.send({
"code": code
});
})