image_node/app.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-04-26 15:22:43 +02:00
let express = require("express");
2020-04-26 16:24:48 +02:00
let morgan = require("morgan");
let fs = require("fs");
let rl = require("readline").createInterface({input: process.stdin, output: process.stdout, terminal: false});
let passwordHash = require('password-hash');
2020-04-26 15:22:43 +02:00
let app = express();
2020-04-26 16:24:48 +02:00
if (!fs.existsSync("users.json")) {
fs.writeFileSync("users.json", "{}");
console.log("Register first user");
addUser();
} else if (Object.keys(JSON.parse(fs.readFileSync("users.json"))).length === 0) {
console.log("No user found, adding a new one");
addUser();
}
async function addUser() {
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));
}
2020-04-26 15:22:43 +02:00
app.use(morgan("dev"))
.use(express.static("public"))
.set("view engine", "pug")
.get("/", (req, res) => {
res.render("index");
});
app.listen(8080)