-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (31 loc) · 1.01 KB
/
server.js
File metadata and controls
38 lines (31 loc) · 1.01 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
'use strict'
const url = require('url');
const http = require('http');
const {getAllProducts, getProduct, createProduct} = require("./controllers/productController");
const server = http.createServer((req, res)=>{
if(req.method === 'GET') {
if (req.url === '/api/products') {
return getAllProducts(req, res);
}
const regularExpression = /\/api\/products\/([0-4]+)/;
const match = req.url.match(regularExpression);
if (match) {
const id = req.url.split('/')[3];
return getProduct(req, res, id);
}
}
else if(req.method === 'POST'){
if (req.url === '/api/products'){
return createProduct(req, res);
}
}
return handleNotFound(res);
})
const handleNotFound = (res) => {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/html');
res.write('The product not found');
res.end();
}
const PORT = process.env.PORT || 5500;
server.listen(PORT, ()=>{`The server starting on port ${PORT}`})