-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholdIndex.js
44 lines (40 loc) · 1.13 KB
/
oldIndex.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
41
42
43
44
// Import modules required to build server
const http = require("http");
const fs = require("fs");
const path = require("path")
const dotenv = require("dotenv")
dotenv.config()
// Let's make a server
const PORT = process.env.PORT || 3300;
const server = http.createServer((req, res) => {
const products = [
{
id: 1,
name: "Laptop",
price: 1000
},
{
id: 2,
name: "Mobile",
price: 500
}
]
if (req.url === "/api/products") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(products))
}
else {
fs.readFile(path.join(__dirname, "public", "index.html"), (err, data) => {
if (err) {
res.writeHead(500, {"Content-Type": "text/html"});
res.end("<h1>Internal Server Error</h1>");
}
else {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(data)
}
})
}
})
// Listen the server
server.listen(PORT, () => console.log(`Server is running on port ${PORT}`))