-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (29 loc) · 928 Bytes
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
const port = 3000;
const jsonParser = bodyParser.json();
const fileName = 'students.json';
// Load data from file
let rawData = fs.readFileSync(fileName);
let data = JSON.parse(rawData);
app.set('views', 'views');
app.set('view engine', 'hbs');
app.use(express.static('public'));
app.get('/', (request, response) => {
response.render('home');
});
// This is a RESTful GET web service
app.get('/students', (request, response) => {
data.sort((a, b) => (a.name > b.name) ? 1 : -1 );
response.send(data);
});
// This is a RESTful POST web service
app.post('/students', jsonParser, (request, response) => {
data.push(request.body);
fs.writeFileSync(fileName, JSON.stringify(data, null, 2));
response.end();
});
app.listen(port);
console.log('server listening on port 3000');