The appearance of the flow in the style of node.js

I want to add some admin utilities to a small web application such as Backup Database. The user clicks the button and the HTTP response returns immediately, although a potentially lengthy process has been started in the background.

In Java, this is likely to be implemented by creating an independent thread in Scala using the Actor. But what is the appropriate idiom in node.js? (code snippet appreciated)

Now I'm re-reading the docs, this really looks like a question from node 101, but this is pretty much where I am on this ... anyway, to clarify this main scenario:

function onRequest(request, response) {
    doSomething();
    response.writeHead(202, headers);
    response.end("doing something");
}

function doSomething(){
     // long-running operation
}

I want the response to return immediately, leaving doSomething () in the background.

, node, . .

, , I/O, node . , , doSomething response.end, , .

+5
2

. , , doSomething() . , onRequest , ", ".

function doSomething() {
    openDatabaseConnection(connectionString, function(conn) {
        // This is called some time later, once the connection is established.
        // Now you can tell the database to back itself up.
    });
}

doSomething , , , , . , , . , , , async , , ; , , .

( , - response doSomething doSomething response.end , , , , , .)

+3

, . , , . , . , , (). ( ) beanstalkd, gearman.

+5

All Articles