Frage

I know the following question might be a little bit vague but still I need some answers about it.

I'm about to implement a JSON based WebService that will feed information to a couple of websites and mobile Apps.

Since most of time we're doing CPU intensive MySQL queries and need to cache data, PHP is no longer enough and we decided to move to Node.js.

How can one plan, manage, implement and maintain complex business logic with Node.js since we're talking about fully async programming and not something run-time oriented like PHP, Java etc...?

I've tried to put something very simple together with Node.js and after some minutes all my code starts be full of nasty callbacks, overkill dependency injection and it becomes a mess. How should I convert the typical synchronous workflow into something Node friendly?

I've noticed most of the Node.js community is looking at premisses as a way to solve this issues, but still I don't see it is enough, its still a mess...

Any tips?

War es hilfreich?

Lösung

Managing complex business logic with node is possible. I can give you some tips :

Code organization point of view

Think async

Easy to say, but when I began node, comming from C programming, it wasn't easy at all.

If you have something like :

read(function(){
    //some stuff
    write(function(){
        //some other stuff
       [etc...]
    }
}

Your code should probably be :

var onRead = function(){
    //some stuff
    write(onWrite);
}
var onWrite = function(){
    //some other stuff
}
read(onRead);

or even better : Make a onRead module ? wich lead me to second advice :

Make modules

A lot of them. And load them on start. A 50 line module can't really be that complicated to read, can it?

It takes some time to get accustomed with this, but you'll feel at home really quickly.

Use modules designed to remove "nested callback hells"

async is one of them. There are many out there.

I don't really use those. Often it's just a workaround for bad design, but they can be really useful in some situations.

Software architecture point of view

Doesn't change as much as you can expect. Mostly you'll want something similar to what you'd do on a stateless php app, apply above tips and it should be OK.

If you want more precise advice, maybe you should paste a code sample which is representative of your problems to let us explain how you can improve it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top