var express = require('express');
var fs = require('fs')
var path = require('path')

var app = express();
var port = process.env.PORT || 8080;

app.get('/callback', function (req, res) {
    var token = req.query.code
    const userToken = {}
      userToken.userToken = token
    fs.writeFile(path.resolve(__dirname, "../data/token.json"), JSON.stringify(userToken), function (err) {
      if (err) return console.log(err);
      console.log(`Token written to file`);
          process.exit()
    });
    res.status(200).send("Request successful")
  });

// start the server
app.listen(port, '0.0.0.0');
console.log('Server started! At http://localhost:' + port);