Skip to content

Instantly share code, notes, and snippets.

@elpuas
Created April 1, 2021 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elpuas/fe532165ffca2d2b7110d4214b245b2a to your computer and use it in GitHub Desktop.
Save elpuas/fe532165ffca2d2b7110d4214b245b2a to your computer and use it in GitHub Desktop.
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