-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
43 lines (35 loc) · 1.15 KB
/
main.js
File metadata and controls
43 lines (35 loc) · 1.15 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
const http = require("http");
const path = require("path");
const fs = require("fs");
const server = http.createServer((req, res) => {
if (req.url === "/") {
fs.readFile(
path.join(__dirname, "public", "index.html"),
(err, content) => {
if(err) throw err;
res.writeHead(200, {"Content-Type": 'text/html'});
res.end(content);
}
);
}
if (req.url === "/about") {
fs.readFile(
path.join(__dirname, "public", "about.html"),
(err, content) => {
if(err) throw err;
res.writeHead(200, {"Content-Type": 'text/html'});
res.end(content);
}
);
}
if (req.url === "/api/users") {
const users = [
{name: "Jason", age: 22},
{name: "Erica", age: 20}
];
res.writeHead(200, {"Content-Type": 'application/json'});
res.end(JSON.stringify(users));
}
});
const PORT = process.env.PORT || 5000
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));