URL routing in Node.js

Homework:

Node Book for Beginners

How do I start with Node.js [closed]

Structuring Node Handlers

Backstory: I wanted to try writing my own framework, but I am having some problems, most likely due to the fact that I do not understand it completely.

I want to achieve a syntax that looks like this:

var app = require('./app'); //this part is understood and it works in my current code.
app.get('/someUrl', function(){ //do stuff here });
app.post('/someOtherUrl', function(){ //do stuff here });

I know an Express structure that has the same syntax, but reading their source code still eludes me.

This may be a trivial task to achieve, but I just can't create it.

Trying require('./app');in a file deeper in the application creates an undefined object, so I assume the server is a singleton object.

So what have I tried?

, - , , .

require();, .

server.js:

var app = module.exports = {
    preProcess: function onRequest(request, response){
        processor.preRequest(request); //this object adds methods on the request object
        var path = urllib.parse(request.url).pathname;
        router.route(urls, path, request, response);
    },
    createServer: function() {
        console.log("Server start up done.");
        return this.server = http.createServer(this.preProcess);
    }
};

exports.app = app;

get().

index.js:

var app = require('./server');
app.createServer().listen('1337');

router.route() router.js. , () /urlThatWasRequested

, . , , , , .

, , , , , , .

!

+5
1

, , :

1) :

var app = module.exports = {
    // some other code
}
exports.app = app;

app app. .

2) app. - :

var app = module.exports = {
    handlers : [],
    route : function(url, fn) {
        this.handlers.push({ url: url, fn: fn });
    },
    preProcess: function onRequest(request, response){
        processor.preRequest(request);
        var path = urllib.parse(request.url).pathname;
        var l = this.handlers.length, handler;
        for (var i = 0; i < l; i++) {
            handler = this.handlers[i];
            if (handler.url == path)
                return handler.fn(request, response);
        }
        throw new Error('404 - route does not exist!');
    },
    // some other code
}

, : if (handler.url == path) , handler.url , path . , .get .post, , GET POST . :

app.route('/someUrl', function(req, res){ //do stuff here });

, , , first URL-, . , (, middlewares). , :

preProcess: function onRequest(request, response){
    var self = this;
    processor.preRequest(request);
    var path = urllib.parse(request.url).pathname;
    var l = self.handlers.length,
        index = 0,
        handler;
    var call_handler = function() {
        var matched_handler;
        while (index < l) {
            handler = self.handlers[index];
            if (handler.url == path) {
                matched_handler = handler;
                break;
            }
            index++;
        }
        if (matched_handler)
             matched_handler.fn(request, response, function() {
                 // Use process.nextTick to make it a bit more scalable.
                 process.nextTick(call_handler);
             });
        else
             throw new Error('404: no route matching URL!');
     };
     call_handler();
},

app.route('/someUrl', function(req, res, next){ 
    //do stuff here
    next(); // <--- this will take you to the next handler
});

( , ).

3) :

('./app'); undefined, , .

. require . undefined, - ( ?).

: , . , , , Express. , !

.get .set . route :

route : function(url, fn, type) {
    this.handlers.push({ url: url, fn: fn, type: type });
},
get : function(url, fn) {
    this.route(url, fn, 'GET');
},
post : function(url, fn) {
    this.route(url, fn, 'POST');
},

, type. (undefined type : ). , . !

+11

All Articles