-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.js
More file actions
38 lines (27 loc) · 881 Bytes
/
2.js
File metadata and controls
38 lines (27 loc) · 881 Bytes
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
/*
Q.2 Create a Backend For Counter Web App with Api Endpoint for Managing Counter Using Express
`/` → send counter data as {counter:counter}
`/increment` → increment counter by 1 and send in response latest data as {counter:counter}
`/decrement` → decrement counter by 1 and send in response latest data as {counter:counter}
Note: Using Express is Mandatory for this Question
*/
const express = require('express');
const bodyParser = require('body-parser');
const port = 5000;
const app = express();
let counter = 0
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.json({ counter });
});
app.post('/increment', (req, res) => {
counter += 1;
res.json({ counter });
});
app.post('/decrement', (req, res) => {
counter -= 1;
res.json({ counter });
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});