From 97264f5e1d28769b27990422fbfb56f8206e2d92 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sun, 26 Apr 2020 20:12:50 +0200 Subject: [PATCH] Move routes to separate files --- app.js | 94 ++++++++-------------------------------------- routes/error.js | 6 +++ routes/images.js | 15 ++++++++ routes/index.js | 11 ++++++ routes/login.js | 31 +++++++++++++++ routes/notFound.js | 11 ++++++ routes/upload.js | 25 ++++++++++++ user.js | 29 ++++++++++++++ 8 files changed, 143 insertions(+), 79 deletions(-) create mode 100644 routes/error.js create mode 100644 routes/images.js create mode 100644 routes/index.js create mode 100644 routes/login.js create mode 100644 routes/notFound.js create mode 100644 routes/upload.js create mode 100644 user.js diff --git a/app.js b/app.js index 5443763..eaf9cfd 100644 --- a/app.js +++ b/app.js @@ -4,22 +4,17 @@ let cookieParser = require("cookie-parser"); let bodyParser = require('body-parser'); let session = require("express-session"); let fs = require("fs"); -let passwordHash = require('password-hash'); -let multer = require("multer"); -let crypto = require("crypto"); -let path = require("path"); +let addUser = require("./user").addUser; + +let indexRoute = require("./routes/index"); +let uploadRoute = require("./routes/upload"); +let loginRoute = require("./routes/login"); +let imagesRoute = require("./routes/images"); +let notFoundRoute = require("./routes/notFound"); +let errorRoute = require("./routes/error"); let app = express(); -let storage = multer.diskStorage({ - destination: './images/', - filename: function (req, file, cb) { - crypto.pseudoRandomBytes(16, function (err, raw) { - if (err) return cb(err) - cb(null, raw.toString('hex') + path.extname(file.originalname)) - }) - }} -); -let upload = multer({storage: storage}); + if (!fs.existsSync("users.json")) { fs.writeFileSync("users.json", "{}"); @@ -30,28 +25,6 @@ if (!fs.existsSync("users.json")) { addUser(); } -async function addUser() { - let rl = require("readline").createInterface({input: process.stdin, output: process.stdout, terminal: false}); - let username; - let password; - file = JSON.parse(fs.readFileSync("users.json")); - do { - username = await new Promise(resolve => rl.question("Username: ", resolve)); - } while (username in file || ["", null].indexOf(username) >= 0); - password = passwordHash.generate(await new Promise(resolve => rl.question("Password: ", resolve))); - file[username] = password; - fs.writeFileSync("users.json", JSON.stringify(file)); -} - - -function isAuth(req, res, next) { - if (req.session.login) { - next(); - } else { - res.redirect("/login"); - } -} - app.use(morgan("dev")) .use(express.static("public")) @@ -65,48 +38,11 @@ app.use(morgan("dev")) })) .set("trust proxy", 1) .set("view engine", "pug") - .get("/", isAuth, (req, res) => { - res.render("index"); - }) - .post("/upload", isAuth, upload.single("image"), (req, res) => { - res.redirect("/images/" + req.file.filename); - }) - .get("/login", (req, res) => { - let fail = false; - if ("fail" in req.query) { - fail = true; - } - res.render("login", {title: "login", fail: fail}); - }) - .post("/login", (req, res) => { - if ("username" in req.body && "password" in req.body) { - file = JSON.parse(fs.readFileSync("users.json")); - if (req.body.username in file && passwordHash.verify(req.body.password, file[req.body.username])) { - req.session.login = true; - req.session.save(); - res.redirect("/"); - } else { - res.redirect("/login?fail"); - } - } else { - res.redirect("/login?fail"); - } - }) - .get("/images/:name", (req, res, next) => { - if ("name" in req.params && fs.existsSync("./images/"+req.params.name)) { - res.sendfile("./images/"+req.params.name); - } else { - next(); - } - }) - .use((req, res) => { - res.status(404); - res.render("404", {url: req.path}); - }) - .use((err, req, res, next) => { - console.error(err.stack); - res.status(500); - res.render("error"); - }) + .use("/", indexRoute) + .use("/upload", uploadRoute) + .use("/login", loginRoute) + .use("/images", imagesRoute) + .use(notFoundRoute) + .use(errorRoute) .listen(8080); diff --git a/routes/error.js b/routes/error.js new file mode 100644 index 0000000..d83df8a --- /dev/null +++ b/routes/error.js @@ -0,0 +1,6 @@ +module.exports = (err, req, res, next) => { + console.error(err.stack); + res.status(500); + res.render("error"); +} + diff --git a/routes/images.js b/routes/images.js new file mode 100644 index 0000000..3dca73f --- /dev/null +++ b/routes/images.js @@ -0,0 +1,15 @@ +let router = require("express").Router(); +let fs = require("fs"); + + +router.get("/:name", (req, res, next) => { + if ("name" in req.params && fs.existsSync("./images/"+req.params.name)) { + res.sendfile("./images/"+req.params.name); + } else { + next(); + } +}); + + +module.exports = router; + diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..0760d20 --- /dev/null +++ b/routes/index.js @@ -0,0 +1,11 @@ +let router = require("express").Router(); +let isAuth = require("../user").isAuth; + + +router.get("/", isAuth, (req, res) => { + res.render("index"); +}); + + +module.exports = router; + diff --git a/routes/login.js b/routes/login.js new file mode 100644 index 0000000..63f1692 --- /dev/null +++ b/routes/login.js @@ -0,0 +1,31 @@ +let router = require("express").Router(); +let fs = require("fs"); +let passwordHash = require('password-hash'); + + +router.get("/", (req, res) => { + let fail = false; + if ("fail" in req.query) { + fail = true; + } + res.render("login", {title: "login", fail: fail}); +}); + +router.post("/", (req, res) => { + if ("username" in req.body && "password" in req.body) { + file = JSON.parse(fs.readFileSync("users.json")); + if (req.body.username in file && passwordHash.verify(req.body.password, file[req.body.username])) { + req.session.login = true; + req.session.save(); + res.redirect("/"); + } else { + res.redirect("/login?fail"); + } + } else { + res.redirect("/login?fail"); + } +}); + + +module.exports = router; + diff --git a/routes/notFound.js b/routes/notFound.js new file mode 100644 index 0000000..81c0cdb --- /dev/null +++ b/routes/notFound.js @@ -0,0 +1,11 @@ +let router = require("express").Router(); + + +router.use((req, res) => { + res.status(404); + res.render("404", {url: req.path}); +}); + + +module.exports = router + diff --git a/routes/upload.js b/routes/upload.js new file mode 100644 index 0000000..07044a4 --- /dev/null +++ b/routes/upload.js @@ -0,0 +1,25 @@ +let router = require("express").Router(); +let isAuth = require("../user").isAuth; +let multer = require("multer"); +let crypto = require("crypto"); +let path = require("path"); + +let storage = multer.diskStorage({ + destination: './images/', + filename: (req, file, cb) => { + crypto.pseudoRandomBytes(16, (err, raw) => { + if (err) return cb(err) + cb(null, raw.toString('hex') + path.extname(file.originalname)); + }); + } +}); +let upload = multer({storage: storage}); + + +router.post("/", isAuth, upload.single("image"), (req, res) => { + res.redirect("/images/" + req.file.filename); +}); + + +module.exports = router; + diff --git a/user.js b/user.js new file mode 100644 index 0000000..3187b9e --- /dev/null +++ b/user.js @@ -0,0 +1,29 @@ +let fs = require("fs"); +let passwordHash = require('password-hash'); + + +async function addUser() { + let rl = require("readline").createInterface({input: process.stdin, output: process.stdout, terminal: false}); + let username; + let password; + file = JSON.parse(fs.readFileSync("users.json")); + do { + username = await new Promise(resolve => rl.question("Username: ", resolve)); + } while (username in file || ["", null].indexOf(username) >= 0); + password = passwordHash.generate(await new Promise(resolve => rl.question("Password: ", resolve))); + file[username] = password; + fs.writeFileSync("users.json", JSON.stringify(file)); +} + +function isAuth(req, res, next) { + if (req.session.login) { + next(); + } else { + res.redirect("/login"); + } +} + + +module.exports.addUser = addUser; +module.exports.isAuth = isAuth; +