-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
32 lines (24 loc) · 898 Bytes
/
app.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
const express = require('express');
var bodyParser = require('body-parser')
const app = express();
app.use(express.json());
var store = []
var counter = [0, 0, 0]
const port = 3000;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.htm');
});
app.use('/images', express.static('assets/images'))
app.get('/api/store', (_req, res) => {
const items = { 'item1' : counter[0], 'item2' : counter[1], 'item3' : counter[2] }
res.json(items)
console.log(`List of items in the store requested by ${_req.ip}`)
});
app.post('/api/store', (req, res) => {
console.log(req.body.message)
var item = req.body.message.split("|")[1]
counter[parseInt(item)]++
res.status(200).send("success")
console.log(`Received new item from ${req.ip}, content: ${JSON.stringify(req.body)}`)
});
app.listen(port, () => console.log(`Node App listening on port ${port}!`));