image_node/app.js

56 lines
1.6 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 cookieParser = require("cookie-parser");
let bodyParser = require('body-parser');
let session = require("express-session");
2020-04-26 16:24:48 +02:00
let fs = require("fs");
2020-04-26 20:12:50 +02:00
let addUser = require("./user").addUser;
2020-04-28 22:31:57 +02:00
require("./cli");
2020-04-26 20:12:50 +02:00
let indexRoute = require("./routes/index");
let uploadRoute = require("./routes/upload");
let loginRoute = require("./routes/login");
2020-04-28 23:16:19 +02:00
let logoutRoute = require("./routes/logout");
2020-04-26 20:12:50 +02:00
let imagesRoute = require("./routes/images");
let listRoute = require("./routes/list");
2020-04-27 21:09:43 +02:00
let removeRoute = require("./routes/remove");
2020-04-26 20:12:50 +02:00
let notFoundRoute = require("./routes/notFound");
let errorRoute = require("./routes/error");
2020-04-26 15:22:43 +02:00
let app = express();
2020-04-26 20:12:50 +02:00
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();
}
2020-04-26 15:22:43 +02:00
app.use(morgan("dev"))
.use(express.static("public"))
.use(bodyParser.urlencoded({ extended: true }))
2020-04-26 23:38:10 +02:00
.use(cookieParser(process.env.SECRET || "Keyboard cat"))
.use(session({
2020-04-26 23:38:10 +02:00
secret: process.env.SECRET || "Keyboard cat",
resave: false,
saveUninitialized: false,
cookie: { secure: "auto" }
}))
.set("trust proxy", 1)
.set("view engine", "pug")
2020-04-26 20:12:50 +02:00
.use("/", indexRoute)
.use("/upload", uploadRoute)
.use("/login", loginRoute)
2020-04-28 23:16:19 +02:00
.use("/logout", logoutRoute)
2020-04-26 20:12:50 +02:00
.use("/images", imagesRoute)
.use("/list", listRoute)
2020-04-27 21:09:43 +02:00
.use("/remove", removeRoute)
2020-04-26 20:12:50 +02:00
.use(notFoundRoute)
.use(errorRoute)
2020-04-26 23:38:10 +02:00
.listen(process.env.PORT || 8080);
2020-04-26 15:22:43 +02:00