Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 为什么Heroku只在本地运行node app.js时才起作用?_Node.js_Heroku_Spotify - Fatal编程技术网

Node.js 为什么Heroku只在本地运行node app.js时才起作用?

Node.js 为什么Heroku只在本地运行node app.js时才起作用?,node.js,heroku,spotify,Node.js,Heroku,Spotify,我有一个使用Node.js和Spotify API构建的应用程序。当我在VS代码上本地运行应用程序时,它会转到localhost:8888。然而,在我将其部署到heroku上后,我在尝试单击Spotify登录时出错,说“无法访问该站点”,localhost拒绝连接。网站链接是vtraxx.herokuapp.com,我将在下面发布一些相关代码: 编辑:我将重定向更改为:现在,当您单击“登录到Spotify”时,不会发生任何事情。我还忘了在config.js文件中编辑重定向,但在这样做之后,仍然没

我有一个使用Node.js和Spotify API构建的应用程序。当我在VS代码上本地运行应用程序时,它会转到localhost:8888。然而,在我将其部署到heroku上后,我在尝试单击Spotify登录时出错,说“无法访问该站点”,localhost拒绝连接。网站链接是vtraxx.herokuapp.com,我将在下面发布一些相关代码:

编辑:我将重定向更改为:现在,当您单击“登录到Spotify”时,不会发生任何事情。我还忘了在config.js文件中编辑重定向,但在这样做之后,仍然没有发生任何事情

EDIT2:通过将重定向更改为:vtraxx.herokuapp.com/login,我可以使登录名在某种程度上起作用。但是现在我得到一个错误,有太多的重定向。我第一次清理了我的饼干,它就要工作了,但最终没有

app.js:

const request = require('request'); // "Request" library
const cors = require('cors');
const querystring = require('querystring');
const cookieParser = require('cookie-parser');
const { config } = require('./config');

var client_id = 'client id'; // Your client id
var client_secret = 'client secret'; // Your secret
var redirect_uri = 'http://localhost:8888/callback'; // Your redirect uri

/**
 * Generates a random string containing numbers and letters
 * @param  {number} length The length of the string
 * @return {string} The generated string
 */
var generateRandomString = function (length) {
    var text = '';
    var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

    for (var i = 0; i < length; i++) {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
    return text;
};

var stateKey = 'spotify_auth_state';

var app = express();

app.use(express.static(__dirname + '/public'))
    .use(cors())
    .use(cookieParser());

app.get('/login', function (req, res) {
    var state = generateRandomString(16);
    res.cookie(stateKey, state);

    // your application requests authorization
    var scope = 'user-read-private user-read-email user-top-read';
    res.redirect(
        'https://accounts.spotify.com/authorize?' +
            querystring.stringify({
                response_type: 'code',
                client_id: client_id,
                scope: scope,
                redirect_uri: redirect_uri,
                state: state
            })
    );
});

app.get('/callback', function (req, res) {
    // your application requests refresh and access tokens
    // after checking the state parameter

    var code = req.query.code || null;
    var state = req.query.state || null;
    var storedState = req.cookies ? req.cookies[stateKey] : null;

    if (state === null || state !== storedState) {
        res.redirect(
            '/#' +
                querystring.stringify({
                    error: 'state_mismatch'
                })
        );
    } else {
        res.clearCookie(stateKey);
        var authOptions = {
            url: 'https://accounts.spotify.com/api/token',
            form: {
                code: code,
                redirect_uri: redirect_uri,
                grant_type: 'authorization_code'
            },
            headers: {
                Authorization: 'Basic ' + new Buffer(client_id + ':' + client_secret).toString('base64')
            },
            json: true
        };

        request.post(authOptions, function (error, response, body) {
            if (!error && response.statusCode === 200) {
                var access_token = body.access_token,
                    refresh_token = body.refresh_token;

                var options = {
                    url: 'https://api.spotify.com/v1/me',
                    headers: { Authorization: 'Bearer ' + access_token },
                    json: true
                };

                var topOptions = {
                    url: 'https://api.spotify.com/v1/me/top/tracks',
                    headers: { Authorization: 'Bearer ' + access_token },
                    json: true
                };

                // use the access token to access the Spotify Web API
                request.get(topOptions, function (error, response, body) {
                    for (let i = 0; i < body.items.length; i++) {
                        console.log(body);
                    }
                });

                // we can also pass the token to the browser to make requests from there
                res.redirect(
                    '/#' +
                        querystring.stringify({
                            access_token: access_token,
                            refresh_token: refresh_token
                        })
                );
            } else {
                res.redirect(
                    '/#' +
                        querystring.stringify({
                            error: 'invalid_token'
                        })
                );
            }
        });
    }
});

app.get('/refresh_token', function (req, res) {
    // requesting access token from refresh token
    var refresh_token = req.query.refresh_token;
    var authOptions = {
        url: 'https://accounts.spotify.com/api/token',
        headers: { Authorization: 'Basic ' + new Buffer(client_id + ':' + client_secret).toString('base64') },
        form: {
            grant_type: 'refresh_token',
            refresh_token: refresh_token
        },
        json: true
    };

    request.post(authOptions, function (error, response, body) {
        if (!error && response.statusCode === 200) {
            var access_token = body.access_token;
            res.send({
                access_token: access_token
            });
        }
    });
});

app.listen(process.env.PORT || 8888, function () {
    console.log('Server is running on port 8888');
});```

I put the localhost:8888 in the spotify dashboard as well. This is my very first project and I'm not exactly sure I know what I'm doing but this is the final step to the finish product and I'm pulling my hair out. This is also my first time using node.js/JavaScript in general
const request=require('request');//“请求”库
const cors=需要(“cors”);
const querystring=require('querystring');
const cookieParser=require('cookie-parser');
const{config}=require('./config');
var client_id=‘client id’;//您的客户id
var client_secret='client secret';//你的秘密
var redirect_uri='1〕http://localhost:8888/callback'; // 您的重定向uri
/**
*生成包含数字和字母的随机字符串
*@param{number}length字符串的长度
*@return{string}生成的字符串
*/
var generateRandomString=函数(长度){
var text='';
可能的变量='ABCDEFGHIjklmnopqrstuvxyzabCDEFGHIjklmnopqrstuvxyzo123456789';
对于(变量i=0;i