88 lines
2 KiB
JavaScript
88 lines
2 KiB
JavaScript
const express = require("express");
|
|
const multer = require("multer");
|
|
const config = require("config");
|
|
const { version } = require("./package.json");
|
|
|
|
const dbHandler = require("./dbHandler.js");
|
|
const upload = multer({ dest: config.get("app.upload") });
|
|
|
|
const PUBLIC_PATH = `${__dirname}/../public`;
|
|
|
|
let server = undefined;
|
|
|
|
const configureRoutes = (app) => {
|
|
app.get("/", (req, res) => {
|
|
res.sendFile(`${PUBLIC_PATH}/index.html`);
|
|
});
|
|
|
|
app.post("/uploadMarker", upload.single("image"), (req, res) => {
|
|
const data = req.body;
|
|
image = req.file;
|
|
|
|
// Extract marker data
|
|
const newMarker = {
|
|
name: data.name,
|
|
description: data.description,
|
|
filename: image.filename,
|
|
long: data.long,
|
|
lat: data.lat,
|
|
};
|
|
// Add marker to db
|
|
dbHandler.addMarker(newMarker);
|
|
});
|
|
|
|
app.get("/fetchMarkers", (req, res) => {
|
|
dbHandler.getAllMarkers((rows) => {
|
|
res.status(200).json(rows);
|
|
});
|
|
});
|
|
|
|
app.get("/updateMarkers", (req, res) => {
|
|
const data = req.query;
|
|
|
|
// data.fromDate comes as a string so we need to parse it before creating a date object based on it
|
|
dbHandler.getUpdatedMarkers(new Date(Number(data.fromDate)), (rows) => {
|
|
res.status(200).json(rows);
|
|
});
|
|
});
|
|
|
|
app.get("/version", (req, res) => {
|
|
res.json({ version });
|
|
});
|
|
};
|
|
|
|
const configureExpress = () => {
|
|
const app = express();
|
|
const port = config.get("app.port");
|
|
|
|
app.use(express.static(PUBLIC_PATH));
|
|
app.use("/imgs", express.static(config.get("app.upload")));
|
|
app.use(express.json()); // for json
|
|
app.use(
|
|
express.urlencoded({
|
|
extended: true,
|
|
limit: "12MB",
|
|
parameterLimit: 100000,
|
|
})
|
|
); // support encoded bodies
|
|
|
|
configureRoutes(app);
|
|
|
|
server = app.listen(port, "0.0.0.0", () => {
|
|
console.log(`Example app listening on port ${port}`);
|
|
});
|
|
|
|
return app;
|
|
};
|
|
|
|
const main = () => {
|
|
process.on("SIGINT", () => {
|
|
server.close(() => {
|
|
console.log("Successfully closed HTTP Server.");
|
|
});
|
|
});
|
|
|
|
configureExpress();
|
|
};
|
|
|
|
main();
|