Skip to content

Instantly share code, notes, and snippets.

@elpuas
Created Apr 1, 2021
Embed
What would you like to do?
const http = require( 'http' )
// import 'fs' a file system module which
// helps interact with files on our server
const fs = require('fs')
// The 'readFileSync' method from 'fs'
// reads the content of each file and returns it.
const homePage = fs.readFileSync('index.html')
const aboutPage = fs.readFileSync('about.html')
const contactPage = fs.readFileSync('contact.html')
const notFoundPage = fs.readFileSync('404.html')
const server = http.createServer( ( req, res ) => {
console.log( req.url )
switch ( req.url ) {
case '/about':
res.end(aboutPage)
break;
case '/contact':
res.end(contactPage)
break;
case '/':
res.end(homePage)
break;
default:
res.writeHead(404)
res.end(notFoundPage)
}
} )
server.listen(3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment