Skip to content

Instantly share code, notes, and snippets.

@Med116
Created May 2, 2014 20:12
Show Gist options
  • Save Med116/8fa7d63faf70da8c2f64 to your computer and use it in GitHub Desktop.
Save Med116/8fa7d63faf70da8c2f64 to your computer and use it in GitHub Desktop.
little router and server for node
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;
}
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);
}
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();
}
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));
}
});
}
}
}
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();
}
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);
.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