0
votes

I have the following Node.JS (ran with Express) code :

let app = express();

app.use(cors());

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 authOptions = {
        url: 'https://accounts.spotify.com/api/token',
        form: {
            code: code,
            redirect_uri: redirectUri,
            grant_type: 'authorization_code'
        },
        headers: {
            'Authorization': 'Basic ' + (new Buffer(clientId + ':' + clientSecret).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;

                fs.writeFile('test.txt', 'HELLO', function (err) {
                    if (err) return console.log(err);
                    console.log('Hello World > helloworld.txt');
                });
            }
        }
    )
});

console.log('Listening on 8888');
app.listen(8888);

The route is used as a callback for a request to the Spotify Web API, thus I can get an access token.

Spotify then redirects to the callback function above, you can see it in the URI by looking at "redirect_uri".

If you need more information about the Spotify Authorization Flow, see here.

Here's the URI I'm using to authenticate my app to Spotify.

https://accounts.spotify.com/authorize?client_id=CLIENT_ID&response_type=code&redirect_uri=http://localhost:8888/callback&scope=user-read-private%20user-read-email%20playlist-modify-public&state=PexBrjEzISHepTp7&show_dialog=false

CLIENT_ID is replaced by my real CLIENT_ID in the request I make

My problem is located to the file writing part :

fs.writeFile('test.txt', 'HELLO', function (err) {
    if (err) return console.log(err);
    console.log('Hello World > helloworld.txt');
});

When the callback route is called by Spotify, I have the string "HELLO" wrote in my text file, so the file writing is functional.

But even if it has finished writing the string, the Chrome page is still running and "pending" on the server. It runs for a few minutes and then crash by saying that the page didn't sent any data. Why ?

I've looked at this page talking about the methods of writing to text files, using writeFile and writeFileAsync, but using both of them didn't solved my problem.

EDIT: I don't really want to stop the Express process! I just want to be able to process another request :)

Any idea ? Thanks in advance :)

2
you need to call res.end() when your request is complete. searching for "express end request" is a resourceful way to find the answerAndy Ray

2 Answers

-1
votes

In your get route you are not sending response, you must send response irrespective of writing a file was successful or not.

Add below code post writing to file (as well as in if error case)

res.send({YOUR_CHOICE_RESPONSE_DATA})

1
votes

You aren't returning anything from your route, try adding res.send({})