Dispatcher with Node JS

I am trying to make a simple server responding to url / page1 and / page 2; This is the dispatcher.js module:

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 an 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 the 'get' property from undefined

Does anyone know why?

+4
source share
2 answers

It could be a typo

 if(this.listener[method][parsedUrl.pathname]) this.listener[method][parsedUrl.pathname](req, res) 

I think this is this.listeners (with -s)


The second question (from comments):

 HttpDispatcher.prototype.on = function(method, url, cb) { this.listeners[method][url] = cb; } 

This way you can check if the URL exists (for example, you already did this) and access the function as you already did.

+3
source

Well, as you offered a job; I have a final question; I added 3 console.log as follows:

 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 1" is displayed correctly by the browser, but in the console I get:

[Function] Found undefined ERROR LISTENER!

So, first the listener is found, but then also called errorListener; if I click to reload the page, on the console I will get only two lines ([Function] and Found), not more than ERROR LISTENER! ... Do you know why the first time I load the page, even if I get the correct result in browser, dispatcher call also erroListener?

0
source

Source: https://habr.com/ru/post/1488060/


All Articles