ruscomap/server/index.js

80 lines
No EOL
1.8 KiB
JavaScript

const express = require("express");
const multer = require("multer");
const config = require("config");
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;
//console.log(data.fromDate)
dbHandler.getUpdatedMarkers(data.fromDate, (rows) => {
res.status(200).json(rows);
});
});
}
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, () => {
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();