Index
Only One Tweak Needed


Because we coded a reusable module we can now easily serve different files.

In this example we'll serve more complex HTML -- a page with a form, and with CSS styling.

Here is the HTML file.

serve-form.js
We create a new file "serve-form.js" that will serve the new HTML.

The only thing we need to tweak is the path.

And we run "serve-form.js" in the normal way to make sure it works.
//serve-form.js

const
	http = require('http'),
	httpStatusCodes = require('http-status-codes'),
	fsModule = require('./file-server.js'),
	path = 'views/rectangle.html',
	htmlType = {'Content-Type': 'text/html'},
	port = 3000;
	
const callbackFct = (req, res) => {
	res.writeHead(httpStatusCodes.OK, htmlType);
	const fileServer = new fsModule.FileServer();
	fileServer.serveTo(path, res);
}

const webServer = http.createServer(callbackFct);
webServer.listen(port);


References