import { createServer } from 'http';
import { readFile } from 'fs';
const handleIncomingRequest = (request, response) => {
// request.url contains the portion of the URL after the domain.
// E.g. for https://ra.co/index.html, request.url would return "/index.html".
console.log('request url', request.url);
// "." refers to the Unix filesystem ".", which represents the current directory.
const filePath = '.' + request.url;
readFile(filePath, (err, content) => {
console.error('error reading file', err);
// Set the response code to 200 (i.e. OK)
// Send the response with the file content in utf-8 format
response.end(content, 'utf-8');
// Initialise server with request listener function handleIncomingRequest
// https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener
// Use port 3004 by convention.
createServer(handleIncomingRequest).listen(3004);