forked from johnpapa/node-hello
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (43 loc) · 1.45 KB
/
index.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// require the http module of node.js
var http = require('http');
// require the dispatcher module
var httpdispatcher = require('httpdispatcher');
var dispatcher = new httpdispatcher();
// define the port of access for your server
const PORT = 8080;
// We need a function which handles requests and send response
function handleRequest(request, response){
try {
// log the request on console
console.log(request.url);
// Dispatch
dispatcher.dispatch(request, response);
} catch(err) {
console.log(err);
}
}
// Create a server
var myFirstServer = http.createServer(handleRequest);
// add some routes
//A sample GET request
dispatcher.onGet("/", function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>Hey, this is the homepage of your server</h1>');
});
dispatcher.onGet("/Eileen", function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('I will protect you from evilJoe');
});
dispatcher.onGet("/evilJoe", function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>All your bases are mine.</h1>');
});
dispatcher.onError(function(req, res) {
res.writeHead(404);
res.end("Error, the URL doesn't exist");
});
// Start the server !
myFirstServer.listen(PORT, function(){
// Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});