1// load up our shiny new route for users
2const userRoutes = require('./users');
3
4const appRouter = (app, fs) => {
5 // we've added in a default route here that handles empty routes
6 // at the base API url
7 app.get('/', (req, res) => {
8 res.send('welcome to the development api-server');
9 });
10
11 // run our user route module here to complete the wire up
12 userRoutes(app, fs);
13};
14
15// this line is unchanged
16module.exports = appRouter;
17
1const userRoutes = (app, fs) => {
2 // variables
3 const dataPath = './data/users.json';
4
5 // READ
6 app.get('/users', (req, res) => {
7 fs.readFile(dataPath, 'utf8', (err, data) => {
8 if (err) {
9 throw err;
10 }
11
12 res.send(JSON.parse(data));
13 });
14 });
15};
16
17module.exports = userRoutes;
18
1const userRoutes = (app, fs) => {
2 //...unchanged ^^^
3
4 // refactored helper methods
5 const readFile = (
6 callback,
7 returnJson = false,
8 filePath = dataPath,
9 encoding = 'utf8'
10 ) => {
11 fs.readFile(filePath, encoding, (err, data) => {
12 if (err) {
13 throw err;
14 }
15
16 callback(returnJson ? JSON.parse(data) : data);
17 });
18 };
19
20 const writeFile = (
21 fileData,
22 callback,
23 filePath = dataPath,
24 encoding = 'utf8'
25 ) => {
26 fs.writeFile(filePath, fileData, encoding, err => {
27 if (err) {
28 throw err;
29 }
30
31 callback();
32 });
33 };
34
35 // READ
36 // Notice how we can make this 'read' operation much more simple now.
37 app.get('/users', (req, res) => {
38 readFile(data => {
39 res.send(data);
40 }, true);
41 });
42};
43
44module.exports = userRoutes;
45
1{
2 "1": {
3 "name": "king arthur",
4 "password": "password1",
5 "profession": "king",
6 "id": 1
7 },
8 "2": {
9 "name": "rob kendal",
10 "password": "password3",
11 "profession": "code fiddler",
12 "id": 2
13 },
14 "3": {
15 "name": "teresa may",
16 "password": "password2",
17 "profession": "brexit destroyer",
18 "id": 6
19 }
20}
21
1// UPDATE
2app.put('/users/:id', (req, res) => {
3 readFile(data => {
4 // add the new user
5 const userId = req.params['id'];
6 data[userId] = req.body;
7
8 writeFile(JSON.stringify(data, null, 2), () => {
9 res.status(200).send(`users id:${userId} updated`);
10 });
11 }, true);
12});
13
1// load up the express framework and body-parser helper
2const express = require('express');
3const bodyParser = require('body-parser');
4
5// create an instance of express to serve our end points
6const app = express();
7
8// we'll load up node's built in file system helper library here
9// (we'll be using this later to serve our JSON files
10const fs = require('fs');
11
12// configure our express instance with some body-parser settings
13// including handling JSON data
14app.use(bodyParser.json());
15app.use(bodyParser.urlencoded({ extended: true }));
16
17// this is where we'll handle our various routes from
18const routes = require('./routes/routes.js')(app, fs);
19
20// finally, launch our server on port 3001.
21const server = app.listen(3001, () => {
22 console.log('listening on port %s...', server.address().port);
23});
24