-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
32 lines (26 loc) · 1.19 KB
/
server.js
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
// require modules
var http = require('http')
, url = require('url')
, path = require('path');
var pathname
, parameters = {};
// declare function to run the server
// - parameter: 'route' - function from router module
// - parameter: the 'handle' object (from index)
function start(route, handle) {
// declare a function to answer requests to the server
function onRequest (request, response) {
pathname = url.parse(request.url).pathname;
parameters.format = path.extname(pathname);
parameters.resource = path.basename(pathname, parameters.format);
console.log('Request received for:"' + parameters.resource + '"; in format: "' + parameters.format + '"');
route(handle, parameters, response, request);
}
// start the server, define 'onRequest' as the callback function
// - if we got a port from enivronment (ie. heroku, or foreman), use it, otherwise use 8888
var port = process.env.PORT || 9999;
http.createServer(onRequest).listen(port);
console.log("Server has started on port " + port + ".");
}
// export our start function (map public method to internal implementation function)
exports.start = start;