Serve css files dynamically in raw node.js

417 views Asked by At

I can servre css files when the browser requests for it, which is like

var pathname = url.parse(req.url, true);
if(pathname=="style.css"){
    //read the css file and write in the response
}

but using this approach i will have to write a router for each css and js file I use. Is there any way to do them dynamically. I have figured out a way which works but seems to be vaulnarable.

var reqArray = pathname.split("/");
if(req.Array[reqArray.length -1].indexOf(".css") !=-1 && fs.existsSync(pathname)){
fs.readFile("./"+pathname, function(err,data){
 //server the file
    }
}

is it okay, or there is any better suggestion. Please don't tell me to use express or any toher framework.

0

There are 0 answers