51 lines
1.0 KiB
JavaScript
51 lines
1.0 KiB
JavaScript
var bodyParser = require('body-parser'); // Data conversion, JSON parsing
|
|
var express = require('express');
|
|
var https = require('https');
|
|
var http = require('http');
|
|
var path = require('path');
|
|
var fs = require('fs');
|
|
|
|
|
|
var app = express();
|
|
|
|
// Set root as current dir
|
|
app.use(express.static("./"));
|
|
|
|
// Use JSON handler when parsing requests
|
|
app.use(express.json());
|
|
|
|
http.createServer(app).listen(8000);
|
|
// https.createServer(options, app).listen(443);
|
|
|
|
// Get root
|
|
app.get('/', function(rq, rs)
|
|
{
|
|
rs.sendFile('./index.html')
|
|
});
|
|
|
|
// Get mates count
|
|
app.get('/mates', function(rq, rs)
|
|
{
|
|
var data = fs.readFileSync('./mates.json');
|
|
var json = JSON.parse(data);
|
|
rs.send(json);
|
|
});
|
|
|
|
// Update mates count
|
|
app.post('/mates', function(rq, rs)
|
|
{
|
|
var data = fs.readFileSync('./mates.json');
|
|
var json = JSON.parse(data);
|
|
json.mates += 1;
|
|
|
|
fs.writeFile('./mates.json', JSON.stringify(json), (err) => {
|
|
if (err)
|
|
{
|
|
rs.sendStatus(500);
|
|
throw err;
|
|
}
|
|
});
|
|
|
|
rs.send(json);
|
|
});
|