diff --git a/package.json b/package.json index 8c43d41..567be10 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,10 @@ "name": "google-photo-album-sync", "version": "1.0.0", "description": "Syncs a google photo album with a harddrive", - "main": "index.js", + "main": "src/sync.js", "type": "module", "scripts": { - "token": "node getToken.js", + "token": "node src/getToken.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ diff --git a/getToken.js b/src/getToken.js similarity index 82% rename from getToken.js rename to src/getToken.js index 7e110b4..51875ac 100644 --- a/getToken.js +++ b/src/getToken.js @@ -2,7 +2,9 @@ import express from "express"; import bodyParser from "body-parser"; import open from "open"; import fetch from "node-fetch"; -import {readFileSync, writeFileSync} from "fs" +import {readFileSync, writeFileSync} from "fs"; +import {dirname} from "path"; +import {fileURLToPath} from "url"; const app = express() app.use(bodyParser.urlencoded({ extended: true })); @@ -11,8 +13,8 @@ const port = 8080; let state = "code"; -// Read and parse JSON -const secrets = JSON.parse(readFileSync("./secrets.json", "utf8")); +// Read and parse JSON. There is no __dirname for modules +const secrets = JSON.parse(readFileSync(`${dirname(fileURLToPath(import.meta.url))}/../secrets.json`, "utf8")); // Express route that prints request body and query parameters app.use("/", (req, res) => { @@ -20,7 +22,7 @@ app.use("/", (req, res) => { case "code": { state = "token"; getToken(req.query); - res.status(200).json(req.query).end(); + res.status(200).send("Done! You can close this window.").end(); break; } case "token": { @@ -32,17 +34,6 @@ app.use("/", (req, res) => { } }); -app.use("/token", (req, res) => { - console.log({ - get: req.query, - post: req.body - }); - res.status(200).json({ - get: req.query, - post: req.body - }).end() -}); - const server = app.listen(port, () => console.log(`Example app listening on port ${port}!`)); const token = "miau";