Created
May 2, 2014 20:12
-
-
Save Med116/8fa7d63faf70da8c2f64 to your computer and use it in GitHub Desktop.
little router and server for node
This file contains 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
module.exports = function(title, content){ | |
var zm = require("zm").zm; | |
var html = new zm.BaseTag().tag("html"); | |
var head = new zm.BaseTag().tag("head"); | |
head.addContent(new zm.BaseTag().tag("link").setAttr("href", "style.css").setAttr("type", "text/css").setAttr("rel","stylesheet")); | |
html.addContent(head); | |
var body = new zm.BaseTag().tag("body"); | |
// here is where content is injected | |
body.akon(new zm.Div(content).setAttr("id","main")); | |
html.akon(body); | |
return html; | |
} | |
This file contains 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
module.exports = function(req,res){ | |
var url = require("url"); | |
var RouteRegex = require("./route_regex.js"); | |
var router = new RouteRegex(); | |
var fs = require("fs"); | |
var path = require("path"); | |
var os = require("os"); | |
var through = require("through"); | |
console.log(url.parse(req.url)); | |
var foo = function (m){ | |
return "I am a Foo"; | |
} | |
var bar = function(m){ | |
return "I am a Bar"; | |
} | |
var profile = function(m){ | |
var name = m[1]; | |
res.end("<h1> Hello My name is: " + name + " </h1>"); | |
} | |
// respond to css | |
router.addRoute("[a-aA-z0-9]{1,50}.css$", function(matches){ | |
var cssFile = matches[0]; | |
var pathToCss = process.cwd() + "/" + cssFile; | |
// takes care of os diffs regarding path delimiters and such | |
pathToCss = path.normalize(pathToCss); | |
console.log(matches); | |
console.log("PATH TO CSS"); | |
console.log(pathToCss) | |
var readable = fs.createReadStream(pathToCss); | |
var write = function(chunk){ | |
this.queue(chunk.toString()); | |
console.log(chunk.toString()); | |
} | |
var end = function(){ | |
this.queue(null); | |
} | |
var thru = through(write,end); | |
req.on("end",function(){ | |
res.pipe(readable.pipe(thru)).pipe(res); | |
res.end(); | |
}); | |
}); | |
router.addRoute("/foo$", foo); | |
router.addRoute("/bar$", bar); | |
router.addRoute("/profile/([a-zA-Z0-9]{1,25})$", profile); | |
router.addRoute("/search/$", require("./search_form")); | |
router.run(req,res); | |
res.end(req.url); | |
} | |
This file contains 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
module.exports = function(req,res, postArray){ | |
var url = require('url'); | |
var RouteRegex = require("./route_regex.js"); | |
var router = new RouteRegex(); | |
router.addRoute("/search/([A-Z a-z0-9]+)", require("./search.js")); | |
router.run(); | |
} |
This file contains 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
module.exports = function(){ | |
return{ | |
"urls" : new Array(), | |
"addRoute" : function(regex,func){ | |
this.urls.push([new RegExp(regex), func]); | |
}, | |
"run": function(req,res){ | |
this.urls.forEach(function(u){ | |
var matches = []; | |
if(matches = req.url.match(u[0])){ | |
res.end(u[1](matches)); | |
} | |
}); | |
} | |
} | |
} |
This file contains 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
module.exports = function(matches){ | |
var zm = require("zm").zm; | |
var layout = require("./layout"); | |
var form = new zm.BaseTag().tag("form").setAttr("action","search").setAttr("method", "post") | |
.addContent(new zm.InputText().setAttr("name","q")) | |
.addContent(new zm.InputText().setAttr("name","state")) | |
.addContent(new zm.InputText().setAttr("name","session").setAttr("value","20132014")) | |
.addContent(new zm.InputText().setAttr("type","submit")) | |
return layout("search title",form).print(); | |
} |
This file contains 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
var http = require("http"); | |
var postObj = require("./postobj.js"); | |
var routeGet = require("./route_get.js"); | |
var routePost = require("./route_post.js"); | |
var server = http.createServer(function(req,res){ | |
if(req.method == "POST"){ | |
var str = ""; | |
req.on("data",function(chunk){ | |
str += chunk.toString(); | |
}); | |
req.on("end",function(){ | |
if(str){ | |
var postArray = postObj(str); | |
routePost(req,res,postArray); | |
}else{ | |
console.log("nothing was posted"); | |
res.end("nothing was posted"); | |
} | |
}); | |
}else if(req.method == "GET"){ | |
routeGet(req,res); | |
}else{ | |
res.end("METHOD WAS NEITHER GET NOR POST, ADD PUT/DELETE HERE IF YOU NEED IT"); | |
} | |
}).listen(3000); |
This file contains 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
.red{ | |
color:red; | |
} | |
#main{ | |
border:5px solid red; | |
margin:5em; | |
padding:5em; | |
background:#fefefe; | |
} | |
body{ | |
background:black; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment