|
1 |
| -"use strict"; |
2 |
| -const express = require("express"); |
3 |
| -const app = express(); |
| 1 | +'use strict' |
| 2 | +const express = require('express') |
| 3 | +const app = express() |
4 | 4 |
|
5 | 5 | // Get the Mongoose models used for querying the database
|
6 |
| -const {User} = require("./models.js"); |
| 6 | +const { User } = require('./models.js') |
7 | 7 |
|
8 | 8 | // A list of the fields that are allowed to be accessed
|
9 |
| -const defaultFields = ["_id", "username", "group"]; |
| 9 | +const defaultFields = ['_id', 'username', 'group'] |
10 | 10 |
|
11 | 11 | // Filter a user object based on the requested fields
|
12 | 12 | const filterFields = async function(req, user) {
|
13 |
| - // We assume the fields are a comma-separated list of field |
14 |
| - // names, if none is specified then we return all fields. |
15 |
| - const fieldKeys = req.query.fields |
16 |
| - ? req.query.fields.split(",") |
17 |
| - : defaultFields; |
| 13 | + // We assume the fields are a comma-separated list of field |
| 14 | + // names, if none is specified then we return all fields. |
| 15 | + const fieldKeys = req.query.fields |
| 16 | + ? req.query.fields.split(',') |
| 17 | + : defaultFields |
18 | 18 |
|
19 |
| - // Generate a new object that contains only those fields. |
20 |
| - const filteredUser = {}; |
21 |
| - for (const field of fieldKeys) { |
22 |
| - // If the field is a function then we expect it to return |
23 |
| - // a promise which we will immediately resolve. |
24 |
| - if (typeof user[field] === "function") { |
25 |
| - filteredUser[field] = await user[field](); |
26 |
| - } else { |
27 |
| - filteredUser[field] = user[field]; |
28 |
| - } |
| 19 | + // Generate a new object that contains only those fields. |
| 20 | + const filteredUser = {} |
| 21 | + for (const field of fieldKeys) { |
| 22 | + // If the field is a function then we expect it to return |
| 23 | + // a promise which we will immediately resolve. |
| 24 | + if (typeof user[field] === 'function') { |
| 25 | + filteredUser[field] = await user[field]() |
| 26 | + } else { |
| 27 | + filteredUser[field] = user[field] |
29 | 28 | }
|
30 |
| - return filteredUser; |
31 |
| -}; |
| 29 | + } |
| 30 | + return filteredUser |
| 31 | +} |
32 | 32 |
|
33 | 33 | // Listen for all GET requests to /users/:id URL (where the
|
34 | 34 | // ID is the ID of the user account)
|
35 |
| -app.get("/users/:id", (req, res) => { |
36 |
| - // Try to find the user by their id (_id field), using the ID |
37 |
| - // parameter from the URL. |
38 |
| - User.findById(req.params.id, async (err, user) => { |
39 |
| - if (err) { |
40 |
| - // The DB returned an error so we return a 500 error |
41 |
| - return res.status(500).end(); |
42 |
| - } |
| 35 | +app.get('/users/:id', (req, res) => { |
| 36 | + // Try to find the user by their id (_id field), using the ID |
| 37 | + // parameter from the URL. |
| 38 | + User.findById(req.params.id, async (err, user) => { |
| 39 | + if (err) { |
| 40 | + // The DB returned an error so we return a 500 error |
| 41 | + return res.status(500).end() |
| 42 | + } |
43 | 43 |
|
44 |
| - if (!user) { |
45 |
| - // No user was found so we return a 404 error |
46 |
| - return res.status(404).end(); |
47 |
| - } |
| 44 | + if (!user) { |
| 45 | + // No user was found so we return a 404 error |
| 46 | + return res.status(404).end() |
| 47 | + } |
48 | 48 |
|
49 |
| - // Return the user to the client (automatically serialized |
50 |
| - // as a JSON string). We need to wait for all of the fields |
51 |
| - // to load before we can return the results. |
52 |
| - res.send(await filterFields(req, user)); |
53 |
| - }); |
54 |
| -}); |
| 49 | + // Return the user to the client (automatically serialized |
| 50 | + // as a JSON string). We need to wait for all of the fields |
| 51 | + // to load before we can return the results. |
| 52 | + res.send(await filterFields(req, user)) |
| 53 | + }) |
| 54 | +}) |
55 | 55 |
|
56 | 56 | // Listen for all GET requests to /users
|
57 |
| -app.get("/users", (req, res) => { |
58 |
| - // Find all of the users in the database collection (we pass in |
59 |
| - // an empty collection as we aren't filtering the results) |
60 |
| - User.find({}, async (err, users) => { |
61 |
| - if (err) { |
62 |
| - // The DB returned an error so we return a 500 error |
63 |
| - return res.status(500).end(); |
64 |
| - } |
| 57 | +app.get('/users', (req, res) => { |
| 58 | + // Find all of the users in the database collection (we pass in |
| 59 | + // an empty collection as we aren't filtering the results) |
| 60 | + User.find({}, async (err, users) => { |
| 61 | + if (err) { |
| 62 | + // The DB returned an error so we return a 500 error |
| 63 | + return res.status(500).end() |
| 64 | + } |
65 | 65 |
|
66 |
| - // Return the array of users to the client (automatically |
67 |
| - // serialized as a JSON string) We need to wait for all |
68 |
| - // of the Promises to resolve for all of the users. |
69 |
| - res.send(await Promise.all(users.map(async (user) => |
70 |
| - await filterFields(req, user)))); |
71 |
| - }); |
72 |
| -}); |
| 66 | + // Return the array of users to the client (automatically |
| 67 | + // serialized as a JSON string) We need to wait for all |
| 68 | + // of the Promises to resolve for all of the users. |
| 69 | + res.send( |
| 70 | + await Promise.all(users.map(async user => await filterFields(req, user))) |
| 71 | + ) |
| 72 | + }) |
| 73 | +}) |
73 | 74 |
|
74 | 75 | // Start the application, listening on port 3000
|
75 |
| -app.listen(3000); |
| 76 | +app.listen(3000) |
0 commit comments