I'm trying to do a simple server answering to url /page1 and /page2; this is the module dispatcher.js:
var HttpDispatcher = function() {
this.listeners = { get: [ ], post: [ ] };
this.errorListener = function() { }
}
HttpDispatcher.prototype.on = function(method, url, cb) {
this.listeners[method].push({
cb: cb,
url: url
});
}
HttpDispatcher.prototype.onGet = function(url, cb) {
this.on('get', url, cb);
}
HttpDispatcher.prototype.onPost = function(url, cb) {
this.on('post', url, cb);
}
HttpDispatcher.prototype.onError = function(cb) {
this.errorListener = cb;
}
HttpDispatcher.prototype.dispatch = function(req, res) {
var parsedUrl = require('url').parse(req.url, true);
var method = req.method.toLowerCase();
if(this.listener[method][parsedUrl.pathname]) this.listener[method][parsedUrl.pathname](req, res)
else this.errorListener(req, res);
}
module.exports = new HttpDispatcher();
and this is the server:
var dispatcher = require('./node_modules/httpdispatcher');
var http = require('http');
dispatcher.onGet("/page1", function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Page One');
});
dispatcher.onPost("/page2", function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Page Two');
});
http.createServer(function (req, res) {
dispatcher.dispatch(req, res);
}).listen(80, '127.0.0.1');
But when i try to execute the server, i get the error:
D:\Works\Web Resources\NODE JS\node_modules\httpdispatcher.js:33
if(this.listener[method][parsedUrl.pathname]) this.listener[method][parsedUr ^ TypeError: Cannot read property 'get' of undefined
Anyone knows why?
It may be a typo
if(this.listener[method][parsedUrl.pathname]) this.listener[method][parsedUrl.pathname](req, res)
I think it is this.listeners (with -s)
2nd question (from comments):
HttpDispatcher.prototype.on = function(method, url, cb) {
this.listeners[method][url] = cb;
}
That way, you can check if URL exists (like you already do), and access the function like you already do.
Ok, the way you suggested works; i have one last question; i've added 3 console.log like this:
HttpDispatcher.prototype.dispatch = function(req, res) {
var parsedUrl = require('url').parse(req.url, true);
var method = req.method.toLowerCase();
console.log(this.listeners[method][parsedUrl.pathname]);
if (this.listeners[method][parsedUrl.pathname])
{
this.listeners[method][parsedUrl.pathname](req, res);
console.log('Found');
}
else
{
this.errorListener(req, res);
console.log('ERROR LISTENER!');
}
}
Now when i request the URL 127.0.0.1/page1 the message "Page One" is correctly displayied by the browser, but in the console i get:
[Function] Found undefined ERROR LISTENER!
So first the listener is found, but then is also called the errorListener; if i click to reload the page, on console i get just two more lines ([Function] and Found), no more ERROR LISTENER!... Do you know why the first time i load a page, even if i get the correct result in the browser, the dispatcher call also the erroListener?