Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require("http");
- var fs = require("fs");
- var mime = require("mime");
- function isSpecificFile(path){
- var regex = /^\/(bower_components|css|img|js|partials)\/.*/;
- return regex.test(path);
- }
- function onRequest(request, response) {
- console.log("Received request: " + request.url);
- if(isSpecificFile(request.url)){
- fs.readFile("app/" + request.url,function(error,data){
- if(error){
- response.writeHead(404,{"Content-type":"text/plain"});
- response.end("Sorry the page was not found");
- }
- else{
- response.writeHead(202,{"Content-type": mime.lookup(request.url)});
- response.end(data);
- }
- });
- }
- else{
- fs.readFile("app/index.html",function(error,data){
- if(error){
- response.writeHead(404,{"Content-type":"text/plain"});
- response.end("Sorry the page was not found");
- }
- else{
- response.writeHead(202,{"Content-type":"text/html"});
- response.end(data);
- }
- });
- }
- }
- http.createServer(onRequest).listen(8000);
- console.log("Server has started.");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement