-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
67 lines (53 loc) · 1.02 KB
/
api.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require("dotenv").config();
const express = require("express");
const morgan = require("morgan");
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use((req, res, next) => {
console.log(
"Request Received: ",
new Date().toLocaleString("en-US", {
timeZone: "Asia/Calcutta",
})
);
next();
});
app.use(morgan("dev"));
app.use(express.json());
//Routes
app.get("/", (req, res) => {
res.json([
{
method: "GET",
path: "/query",
},
{
method: "POST",
path: "/body",
},
{
method: "GET",
path: "/ping",
},
]);
});
app.get("/query", (req, res) => {
res.send(req.query);
});
app.post("/body", (req, res) => {
res.json(req.body);
});
app.get("/ping", (req, res) => {
res.send("pong");
});
//Error Handling
app.use((req, res, next) => {
const error = new Error("Not Found");
error.status = 404;
next(error);
});
//Server Config
var server = app.listen(PORT, () => {
console.log(`Listening At ${PORT} ...`);
});