-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (40 loc) · 1.35 KB
/
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
37
38
39
40
import fastify from 'fastify';
import fs from 'fs';
const server = fastify();
var deleteall = {
todos: []
};
/* Append string to file instead of overwriting it.
fs.readFile('todos.json', 'utf8', function readFileCallback(err, data){
if (err){
console.log(err);
} else {
obj = JSON.parse(data); //now it an object
obj.table.push({id: 2, square:3}); //add some data
json = JSON.stringify(obj); //convert it back to json
fs.writeFile('todos.json', json, 'utf8', callback); // write it back
}});
*/
server.get('/', async (request, reply) => {
return 'Hey, available endpoints: /getall, /deleteall, /edit, /add, /delete\n';
});
// Get all ToDos endpoint, this is the main endpoint, it'll get more requests than other endpoints.
server.get('/getall', async (request, reply) => {
let allTodos = JSON.parse(fs.readFileSync('todos.json', 'utf8'));
return allTodos.todos.join(", ");
});
server.get('/deleteall', async (request, reply) => {
var json = JSON.stringify(deleteall);
fs.writeFile('todos.json', json, 'utf8', (err) => console.log(err));
return 'File overwritten.';
});
server.listen(8080, (err, address) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Server listening at ${address}`);
});
function callback(arg0, json, arg2, callback) {
throw new Error('Function not implemented.');
}