-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
53 lines (40 loc) · 1.46 KB
/
Copy pathserver.js
File metadata and controls
53 lines (40 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var http = require("http");
var url = require("url");
/*
Function start
Starts the server, and define the callback function 'on Request'
or createServer method.
Receive two parameters:
Method route (defined on module router.js) and the array handle
that links the slugs to the methods on requestHandlers module.
*/
function start(route, handle) {
// onRequest is the callback function that responds request
// sent by user when try to load a page on client side
// See documentation about in
// http://nodejs.org/api/http.html#http_event_request
function onRequest(request, response) {
//Parse the url to get the slug
var pathname=url.parse(request.url).pathname; // get pathname
console.log("Request for "+pathname+" received.");
// Call the router with parameters:
// handle: the array that link slug to method
// pathname: the slug
// response: object created by HTTP Server, see docs on
// request
// http://nodejs.org/api/http.html#http_class_http_serverresponse
route(handle, pathname, response, request);
}
// Method to create a server and define the callback function
// that answer the requests sent by users.
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
/*
This line export the function start as an object that works as
a method of module.
i.e. on the other file that call this method:
var server = require("./server");
server.start();
*/