-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
39 lines (36 loc) · 1.17 KB
/
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
31
32
33
34
35
36
37
38
39
const http = require('http');
const productController = require('./controllers/product');
const httpServer = http.createServer((req, res) => {
if (req.url === '/api/products' && req.method === 'GET') {
productController.getPRoducts(req, res);
} else if (
req.url.match(/\/api\/products\/([0-9]+)/) &&
req.method === 'GET'
) {
const id = req.url.split('/')[3];
productController.getOneProduct(req, res, id);
} else if (req.url === '/api/products' && req.method === 'POST') {
productController.createProduct(req, res);
} else if (
req.url.match(/\/api\/products\/([0-9]+)/) &&
req.method === 'PUT'
) {
const id = req.url.split('/')[3];
productController.updateProduct(req, res, id);
} else if (
req.url.match(/\/api\/products\/([0-9]+)/) &&
req.method === 'DELETE'
) {
const id = req.url.split('/')[3];
productController.deleteProduct(req, res, id);
} else {
res.writeHead(404, {
'Content-Type': 'application/json',
});
res.end(JSON.stringify({ message: 'route not found' }));
}
});
const PORT = process.env.PORT || 3000;
httpServer.listen(PORT, () => {
console.log(`Server running on PORT ${PORT}`);
});