Created
June 27, 2013 09:06
-
-
Save ecochan/5875098 to your computer and use it in GitHub Desktop.
NodeJS
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 server = require("./server"); | |
var router = require("./router"); | |
var requestHandlers = require("./requestHandlers"); | |
var handle = {} | |
handle["/"] = requestHandlers.start; | |
handle["/start"] = requestHandlers.start; | |
handle["/upload"] = requestHandlers.upload; | |
server.start(router.route , handle); |
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
function start (response) { | |
console.log("Request handler 'start' was called."); | |
response.writeHead(200 , {"Content-Type": "text/plain"}); | |
response.write("HOLLO SATRT!"); | |
response.end(); | |
} | |
function upload (response) { | |
console.log("Request handler 'upload' was called."); | |
response.writeHead(200 , {"Content-Type": "text/plain"}); | |
response.write("HELLO UOLOAD!"); | |
response.end(); | |
} | |
exports.start = start; | |
exports.upload = upload; |
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
function route (handle , pathname , response) { | |
console.log("About to route a request for " + pathname); | |
if(typeof handle[pathname] ==="function"){ | |
return handle[pathname](response); | |
} else { | |
console.log("No request handler found for " + pathname); | |
response.writeHead(200 , {"Content-Type": "text/plain"}); | |
response.write("404 NOT FOUND!"); | |
response.end(); | |
} | |
} | |
exports.route = route; |
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 url = require("url") | |
function start(route , handle){ | |
function onRequest(request , response){ | |
var pathname = url.parse(request.url).pathname; | |
console.log("Request for " + pathname + " received."); | |
route(handle , pathname , response); | |
} | |
http.createServer(onRequest).listen(8888); | |
console.log("server has started!"); | |
} | |
exports.start = start; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment