Created
April 1, 2021 21:01
-
-
Save elpuas/fe532165ffca2d2b7110d4214b245b2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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