-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
28 lines (25 loc) · 751 Bytes
/
app.js
File metadata and controls
28 lines (25 loc) · 751 Bytes
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
const fs = require('fs');
const http = require('http');
const port = process.env.PORT || 5000;
//Creazione web server
const server = http.createServer((request, response) => {
const routing = {
'/': 'index.html',
'/home': 'index.html',
'/contatti': 'contatti.html'
};
response.setHeader('Content-Type', 'text/html');
render(response, routing[request.url]);
});
function render(response, htmlfile) {
fs.stat(`./${htmlfile}`, (err, stats) => {
if(stats) {
response.statusCode = 200;
fs.createReadStream(htmlfile).pipe(response);
} else {
response.statusCode = 404;
response.end('Risorsa non trovata');
}
})
}
server.listen(port);