const server = http.createServer((req, res) => {
fs.readFile(path, (err, contents) => {
res.write(contents);
res.end();
});
});
server.listen(port);
//file-server-tool.js const fs = require('fs'); class FileServerTool { static serveTo(path, response) { //Read file and serve to response object fs.readFile(path, (err, contents) => { if (err) throw err; response.write(contents); response.end(); }); } } //-------------------------------- exports.FileServerTool = FileServerTool;
//serve-file-2.js const http = require('http'), httpStatusCodes = require('http-status-codes'), {FileServerTool} = require('./file-server-tool.js'), path = 'views/hello.html', htmlType = {'Content-Type': 'text/html'}, port = 3000; const callbackFct = (req, res) => { res.writeHead(httpStatusCodes.OK, htmlType); FileServerTool.serveTo(path, res); } const webServer = http.createServer(callbackFct); webServer.listen(port);
>node serve-file-2