-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (31 loc) · 924 Bytes
/
index.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
const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
const routes = require('./routes/routes');
const errorHandler = require('./utils/error_handler');
const PORT = 8080;
const connection = (req, res) => {
const parsedUrl = url.parse(req.url, true);
const pathname = parsedUrl.pathname;
if(pathname.startsWith('/public')) {
const filePath = path.join(__dirname, pathname);
fs.readFile(filePath, (error, data) => {
if(error) {
errorHandler.handle404(res);
}
else {
res.writeHead(200);
res.write(data);
res.end();
}
});
}
else {
routes.handleRequest(req, res);
}
};
const server = http.createServer(connection);
server.listen(PORT, () => {
console.log(`sever started on port ${PORT}`);
});