Last active
December 30, 2015 21:39
-
-
Save congjf/7888865 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
NodeJS Learning |
This file contains hidden or 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 listener = function(request, response) { | |
response.writeHead(200, { | |
"Content-Type": "text/plain" | |
}); | |
response.write("Hello World"); | |
response.end(); | |
}; | |
var server = http.createServer(listener); | |
server.listen('8000'); | |
console.log('server: 127.0.0.1:8000'); |
This file contains hidden or 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
// Event Driven test... | |
// Output: | |
// Server has started. | |
// Reuest received. (When you request the server through browser) | |
var http = require("http"); | |
function onRequest(request, response) { | |
console.log("Request received."); | |
response.writeHead(200, {"Content-Type": "text/plain"}); | |
response.write("Hello World"); | |
response.end(); | |
} | |
http.createServer(onRequest).listen(8888); | |
console.log("Server has started."); |
This file contains hidden or 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
// This is 'server.js' | |
var http = require("http"); | |
function start() { | |
function onRequest(request, response) { | |
console.log("Request received."); | |
response.writeHead(200, {"Content-Type": "text/plain"}); | |
response.write("Hello World"); | |
response.end(); | |
} | |
http.createServer(onRequest).listen(8888); | |
console.log("Server has started."); | |
} | |
exports.start = start; |
This file contains hidden or 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
// This is 'index.js' | |
// just for import and use the module 'server' | |
var server = require("./server"); | |
server.start(); |
This file contains hidden or 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
// url.parse about | |
url.parse(string).query | |
| | |
url.parse(string).pathname | | |
| | | |
| | | |
------ ------------------- | |
http://localhost:8888/start?foo=bar&hello=world | |
--- ----- | |
| | | |
| | | |
querystring(string)["foo"] | | |
| | |
querystring(string)["hello"] |
This file contains hidden or 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
// url.parse sample | |
var http = require("http"); | |
var url = require("url"); | |
function start() { | |
function onRequest(request, response) { | |
// url pathname | |
var pathname = url.parse(request.url).pathname; | |
console.log("Request for " + pathname + " received."); | |
response.writeHead(200, {"Content-Type": "text/plain"}); | |
response.write("Hello World"); | |
response.end(); | |
} | |
http.createServer(onRequest).listen(8888); | |
console.log("Server has started."); | |
} | |
exports.start = start; | |
// | |
// Output: | |
// | |
// Server has started. | |
// Request for / received. | |
// Request for /favicon.ico received. | |
// Request for /213 received. | |
// Request for /favicon.ico received. |
This file contains hidden or 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"); | |
server.start(router.route); |
This file contains hidden or 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(pathname) { | |
console.log("About to route a request for " + pathname); | |
} | |
exports.route = route; |
This file contains hidden or 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"); | |
// Injecte the dependency of the Router module | |
function start(route) { | |
function onRequest(request, response) { | |
var pathname = url.parse(request.url).pathname; | |
console.log("Request for " + pathname + " received."); | |
route(pathname); | |
response.writeHead(200, {"Content-Type": "text/plain"}); | |
response.write("Hello World"); | |
response.end(); | |
} | |
http.createServer(onRequest).listen(8888); | |
console.log("Server has started."); | |
} | |
exports.start = start; |
This file contains hidden or 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 hidden or 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() { | |
console.log("Request handler 'start' was called."); | |
} | |
function upload() { | |
console.log("Request handler 'upload' was called."); | |
} | |
exports.start = start; | |
exports.upload = upload; |
This file contains hidden or 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
// handle[...] is a function | |
// pathname is a string | |
function route(handle, pathname) { | |
console.log("About to route a request for " + pathname); | |
if (typeof handle[pathname] === 'function') { | |
handle[pathname](); | |
} else { | |
console.log("No request handler found for " + pathname); | |
} | |
} | |
exports.route = route; |
This file contains hidden or 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.writeHead(200, { | |
"Content-Type": "text/plain" | |
}); | |
response.write("Hello World"); | |
response.end(); | |
} | |
http.createServer(onRequest).listen(8888); | |
console.log("Server has started."); | |
} | |
exports.start = start; |
This file contains hidden or 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
// requestHandler.js | |
function start() { | |
console.log("Request handler 'start' was called."); | |
return "Hello Start"; | |
} | |
function upload() { | |
console.log("Request handler 'upload' was called."); | |
return "Hello Upload"; | |
} | |
exports.start = start; | |
exports.upload = upload; |
This file contains hidden or 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
// router.js | |
function route(handle, pathname) { | |
console.log("About to route a request for " + pathname); | |
if (typeof handle[pathname] === 'function') { | |
return handle[pathname](); | |
} else { | |
console.log("No request handler found for " + pathname); | |
return "404 Not found"; | |
} | |
} | |
exports.route = route; |
This file contains hidden or 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
// server.js | |
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."); | |
response.writeHead(200, {"Content-Type": "text/plain"}); | |
var content = route(handle, pathname) | |
response.write(content); | |
response.end(); | |
} | |
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