server html static files into node application
<!DOCTYPE html> <html> <head> <title>Index Us</title> <LINK REL=StyleSheet href="styles.css" TYPE="text/css" MEDIA=screen> </head> <body> <h1>Home</h1> </body> </html>
<!DOCTYPE html> <html> <head> <title>About Us</title> <LINK REL=StyleSheet href="styles.css" TYPE="text/css" MEDIA=screen> </head> <body> <h1>About Us</h1> </body> </html>
<!DOCTYPE html> <html> <head> <title>Contact Us</title> <LINK REL=StyleSheet href="styles.css" TYPE="text/css" MEDIA=screen> </head> <body> <h1>Contact Us</h1> </body> </html>
h1 { color: red; }
var http = require('http'); var fs = require('fs'); http.createServer(function(req,res){ var content = ''; var type = ''; if(req.url === "/") { content = fs.readFileSync('./index.html'); type = 'text/html'; } else if(req.url === "/aboutus") { content = fs.readFileSync('./aboutus.html'); type = 'text/html'; } else if(req.url === "/contactus") { content = fs.readFileSync('./contactUs.html'); type = 'text/html'; } else if(req.url === "/styles.css") { content = fs.readFileSync('./styles.css'); type = 'text/css'; } res.writeHead(200 , {'Content-type': type} ); res.end(content); }).listen(9000,'127.0.0.1'); console.log('server is running');