-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
30 lines (27 loc) · 901 Bytes
/
server.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
const express = require("express");
const app = express();
const fs = require("fs");
const port = process.env.PORT || "3000";
app.get("/users", function (req, res) {
fs.readFile(__dirname + "/" + "users.json", "utf8", function (err, data) {
res.setHeader("Content-Type", "application/json");
res.send(data);
});
});
app.get("/users/:id", function (req, res) {
fs.readFile(__dirname + "/" + "users.json", 'utf8', function (err, data) {
const users = JSON.parse(data);
let user = null;
for (let key in users) {
if (users[key].id === req.params.id) {
user = users[key];
break;
}
}
res.setHeader("Content-Type", "application/json");
res.send(user);
});
});
app.listen(port, function () {
console.log("Server is up and running at http://localhost:%s", port);
});