-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
31 lines (27 loc) · 870 Bytes
/
server.js
File metadata and controls
31 lines (27 loc) · 870 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
const express = require('express')
const fs = require('fs');
const bodyParser = require('body-parser')
// Initialize what you need
const app = express()
const data = 'data.json'
app.use(express.static('public'))
app.use(bodyParser.json());
// Check if data file exists. If it does read it, else create one
if(!fs.existsSync(data)){
var inputs = Array(600).fill(0).map(() => Array(800).fill(0))
fs.writeFile(data, JSON.stringify(inputs), 'utf8', function () {});
}
else{
var inputs = JSON.parse(fs.readFileSync(data))
}
// Get data from the frontend and save it
app.post('/', (req, res)=> {
inputs[req.body.y][req.body.x]++
fs.writeFile(data, JSON.stringify(inputs), 'utf8', function () {});
if(!req.body)
{
return res.status(400).send({status: "failed"})
}
res.status(200).send({status: 'received'})
});
app.listen(3000)