Homework done:
How do I get started with Node.js [closed]
Backstory: I wanted to try and write my own framework but I’m running into some troubles, most likely due to not understanding it fully.
What I want to achieve is 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 of the Express-framework that has this same syntax but reading their source code still eludes me.
This might be a trivial task to achieve but I simply can’t produce it, yet.
Trying to require('./app'); in a file deeper in the application produces a undefined object, so I’m guessing that a server is a singleton object.
So what have I tried?
My current code looks like this, and somehow I feel like this is the way to go, but I can’t apparently do it like this.
I’m omitting all the require(); statements to keep it more readable.
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;
At the time of writing I’m experimenting with extending this object with a get() method.
index.js:
var app = require('./server');
app.createServer().listen('1337');
the router.route() bit basically sends the request onward into the application and inside the router.js-file I do some magic and route the request onward to a function that maps (so far) to the /urlThatWasRequested
This is the behavior I’d like to leave behind.
I know this might be a pretty tall order but all my code is easily discardable and I’m not afraid of rewriting the entire codebase as this is my own project.
I hope this is sufficient in explaining my question otherwise, please say what I should add to make this a bit more clear.
Thanks in advance!
I’m not exactly sure what your question is, but here’s some thoughts:
1) You are creating a circular reference here:
You add
appas a property ofapp. There’s no need for the last line.2) You need to hold handlers in
app. You can try something like this:Note that you may alter this line:
if (handler.url == path)in such a way thathandler.urlis a regular expression and you testpathagainst it. Of course you may implement.getand.postvariants, but from my experience it is easier to check whether a request isGETorPOSTinside the handler. Now you can use the code above like this:The other thing is that the code I’ve shown you only fires the first handler for a given URL it matches. You would probably want to make more complex routes involving many handlers (i.e. middlewares). The architecture would be a bit different in that case, for example:
Now inside your route you can use
which will take you to another handler (or throw exception if there are no more handlers).
3) About these words:
It isn’t true.
requirealways returns the reference to the module object. If you seeundefined, then you’ve messed up something else (altered the module itself?).Final note: I hope it helps a bit. Writing your own framework can be a difficult job, especially since there already are excelent frameworks like Express. Good luck though!
EDIT
Implementing
.getand.setmethods is actually not difficult. You just need to alterroutefunction like this:and then in routing algorithm you check whether
typeproperty is defined. If it is not then use that route (undefinedtype means: always route). Otherwise additionally check if a request’s method matches type. And you’re done!