ruscomap/server/index.js

86 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-09-21 00:09:34 +02:00
const express = require("express");
const multer = require("multer");
const config = require("config");
2024-09-21 00:20:32 +02:00
const dbHandler = require("./dbHandler.js");
2024-09-21 00:09:34 +02:00
const upload = multer({ dest: config.get("app.upload") });
2024-09-21 00:36:00 +02:00
const PUBLIC_PATH = `${__dirname}/../public`;
2024-09-21 00:20:32 +02:00
let server = undefined;
const configureRoutes = (app) => {
app.get("/", (req, res) => {
2024-09-21 00:36:00 +02:00
res.sendFile(`${PUBLIC_PATH}/index.html`);
2024-09-21 00:20:32 +02:00
});
2024-09-21 01:31:23 +02:00
2024-09-21 00:20:32 +02:00
app.post("/uploadMarker", upload.single("image"), (req, res) => {
const data = req.body;
image = req.file;
2024-09-21 01:31:23 +02:00
2024-09-21 00:20:32 +02:00
// 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);
});
2024-09-21 01:31:23 +02:00
2024-09-21 00:20:32 +02:00
app.get("/fetchMarkers", (req, res) => {
dbHandler.getAllMarkers((rows) => {
res.status(200).json(rows);
});
});
2024-09-21 01:31:23 +02:00
2024-09-21 00:20:32 +02:00
app.get("/updateMarkers", (req, res) => {
const data = req.query;
2024-09-21 01:31:23 +02:00
console.log(data.fromDate)
console.log()
dbHandler.getUpdatedMarkers(new Date(Number(data.fromDate)), (rows) => {
2024-09-21 00:20:32 +02:00
res.status(200).json(rows);
});
});
2024-09-21 01:31:23 +02:00
};
2024-09-21 00:16:03 +02:00
const configureExpress = () => {
2024-09-21 00:20:32 +02:00
const app = express();
const port = config.get("app.port");
2024-09-21 00:16:03 +02:00
2024-09-21 00:36:00 +02:00
app.use(express.static(PUBLIC_PATH));
2024-09-21 00:20:32 +02:00
app.use("/imgs", express.static(config.get("app.upload")));
app.use(express.json()); // for json
app.use(
2024-09-21 01:31:23 +02:00
express.urlencoded({
extended: true,
limit: "12MB",
parameterLimit: 100000,
})
2024-09-21 00:20:32 +02:00
); // support encoded bodies
2024-09-21 01:31:23 +02:00
configureRoutes(app);
server = app.listen(port, "0.0.0.0", () => {
2024-09-21 00:20:32 +02:00
console.log(`Example app listening on port ${port}`);
2024-09-21 00:09:34 +02:00
});
2024-09-21 00:20:32 +02:00
return app;
2024-09-21 01:31:23 +02:00
};
2024-09-21 00:20:32 +02:00
const main = () => {
2024-09-21 01:31:23 +02:00
process.on("SIGINT", () => {
2024-09-21 00:20:32 +02:00
server.close(() => {
2024-09-21 01:31:23 +02:00
console.log("Successfully closed HTTP Server.");
2024-09-21 00:20:32 +02:00
});
2024-09-21 00:09:34 +02:00
});
2024-06-30 22:56:19 +02:00
2024-09-21 01:31:23 +02:00
configureExpress();
};
2024-09-21 00:20:32 +02:00
2024-09-21 01:31:23 +02:00
main();