-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
27 lines (22 loc) · 713 Bytes
/
server.js
File metadata and controls
27 lines (22 loc) · 713 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
const http = require('http');
const path = require('path');
const fs = require('fs');
const mime = require('mime');
const port = 3000;
const publicDir = path.join(__dirname, 'public');
const server = http.createServer((req, res) => {
let filePath = path.join(publicDir, req.url === '/' ? 'index.html' : req.url);
fs.stat(filePath, (err, stat) => {
if (err) {
res.statusCode = 404;
res.end('Not Found');
return;
}
const contentType = mime.getType(filePath) || 'text/plain';
res.setHeader('Content-Type', contentType);
fs.createReadStream(filePath).pipe(res);
});
});
server.listen(port, () => {
console.log(`Servidor escuchando en http://localhost:${port}`);
});