Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is a basic node js server, serve the web page,font ,image and video from public folder...
const http = require("http"),
fs = require("fs"),
path = require("path"),
log = console.log;
//this will response with file type with specifiq content and encode
function response(res,file,contentType,encode){
if(file != "404"){
res.writeHead(200,{"Content-type":contentType});
if(encode){
var stream = fs.createReadStream(path.join(__dirname,"public",file),encode);
}else{
var stream = fs.createReadStream(path.join(__dirname,"public",file));
}
stream.pipe(res);
}else{
res.writeHead(404,{"Content-type":"text/html"});
res.end(`
${contentType}
`);
}
}
//response as text
function responseText(res,file,contentType){ response(res,file,contentType,"utf-8"); }
//response as binary
function responseBinary(res,file,contentType){response(res,file,contentType); }
//check the filetype and respose as the type..
function checkReqAndSend(req,res){
if(req.match(/\.css$/)){
responseText(res,req,"text/css");
}else if(req.match(/\.js$/)){
responseText(res,req,"application/javascript");
}else if(req.match(/\.json$/)){
responseText(res,req,"application/json");
}else if(req.match(/\.html$/)){
responseText(res,req,"text/html");
}else if(req.match(/\.svg$/)){ //svg Binary file
responseBinary(res,req,"image/svg+xml");
}else if(req.match(/\.woff2$/)){
responseBinary(res,req,"application/font-woff2");
}else if(req.match(/\.woff$/)){
responseBinary(res,req,"application/font-woff woff");
}else if(req.match(/\.eot$/)){
responseBinary(res,req,"application/vnd.ms-fontobject eot");
}else if(req.match(/\.ttf$/)){
responseBinary(res,req,"application/octet-stream");
}else if(req.match(/\.mp4$/)){
responseBinary(res,req,"video/mp4");
}else if(req.match(/\.flv$/)){
responseBinary(res,req,"video/x-flv");
}else if(req.match(/\.map$/)){ //done
responseBinary(res,req,"application/octet-stream");
}else if(req.match(/\.jpg$/)){
responseBinary(res,req,"image/jpg");
}else if(req.match(/\.png$/)){
responseBinary(res,req,"image/png");
}else if(req.match(/\.jpeg$/)){
responseBinary(res,req,"image/jpeg");
}else if(req.match(/^\/[a-zA-Z]{1}[a-zA-Z0-9]{2,8}$/)){
var file = path.join(__dirname,"public",req)+".html";
fs.access(file,function(err){
if(err){
response(res,"404",
`<h1 style="color:green; font-size:5em; font-weight:100;">
Ok Successful route But We Don't find the file...
</h1>`);
}else{
responseText(res,`${req}.html`,"text/html");
}
});
}else{
response(res,"404",`<h1 style="color:red; font-size:5em; font-weight:100;">Error Request</h1>`);
}
}
//make the server...
http.createServer(function(req,res){
log(req.url);
if(req.url === "/"){
res.writeHead(200,{"Content-type":"text/html"});
fs.readFile(path.join(__dirname,"public","index.html"),function(err,data){
err ? log("Error sending html") : res.end(data);
});
}else{
checkReqAndSend(req.url,res);
}
}).listen(3000,function(err){
if(!err){log("server listen on localhost:3000");}
else{log("Error to create server");}
});
@kousherAlam

This comment has been minimized.

Copy link
Owner Author

kousherAlam commented Jan 21, 2017

I just learning nodeJs, and this is a basic kind of server, that can serve static file. if i again work this file, i will work on checkReqAndSend(req,res) part(i think)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.