-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (30 loc) · 895 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//[Import]
console.time("Load time");
const express = require("express");
const bodyParser = require("body-parser"); //better request parsing
//Info:
const data = require("./data.json");
const port = 8080;
const url = `http://localhost:${port}/`;
//[Initialize app]
const app = express();
const router = express.Router();
//[Define aid tools]
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// [Handle requests]
router.get("/api", async (req, res) => {
try {
if (data) return res.status(200).json(data); //OK
res.sendStatus(410); //Gone
} catch (error) {
console.log(error);
}
return res.sendStatus(500); //Internal Server Error
});
app.use(router);
app.get("*", (req, res) => res.sendStatus(404)); //Page Not Found on any other paths
//[Launch app]
app.listen(port);
console.log("App launched at: " + url);
console.timeEnd("Load time");