-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
50 lines (38 loc) · 1.19 KB
/
app.js
File metadata and controls
50 lines (38 loc) · 1.19 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
// This sets up the back-end (I am using NodeJS and ExpressJS)
const express = require('express');
const fs = require('fs');
const bodyParser = require('body-parser');
// These import the JSON files I am using to store data
// Using JSON files: https://www.geeksforgeeks.org/explain-about-read-and-write-of-a-file-using-javascript/
const test = require('./test.json');
const app = express();
app.use(express.static('client'));
app.use(bodyParser.json());
// Part 1 endpoints
app.get('/test/get', (req, res) => {
res.send(test[0]);
});
app.get('/test/get/:index', (req, res) => {
const index = req.params.index;
res.send(test[index]);
});
app.post('/test/new', (req, res) => {
const message = req.body.message;
test.push(message);
fs.writeFile('test.json', JSON.stringify(test, null, 2), function (err) {
if (err) throw err;
});
res.status(200);
res.end();
});
app.post('/test/remove', (req, res) => {
const index = req.body.index;
test.splice(index, 1);
fs.writeFile('test.json', JSON.stringify(test, null, 2), function (err) {
if (err) throw err;
});
res.status(200);
res.end();
});
// Part 2 endpoints
module.exports = app;