Node.js,Express.js-如何提供延迟响应 [英] Nodejs, expressjs - how to serve delayed response

查看:792
本文介绍了Node.js,Express.js-如何提供延迟响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Web服务,为此我正在使用nodejs,phantomjs和expressjs.我正在学习这三个.

I am building a webservice, for which i am using nodejs, phantomjs and expressjs. I am learning all the three.

我想在处理客户查询后向他们提供延迟响应.例如,

I want to serve a delayed response to the clients after processing their query. Like for example,

我正在处理来自客户端的某些输入,然后,我想在后端处理数据,平均需要大约10秒钟.然后,我想将此页面提供给客户.

I am processing certain inputs from my client, then, i want to process the data at the backend which will take approx 10 sec on an avg. Then i wanted to serve this page to the client.

节点中是否可以发送多个响应到同一请求或延迟的响应,以便模板将自动更新内容.

Is it possible in node to send multiple responses to the same request or delayed responses so that the template will automatically update the contents.

或者,我应该使用相同的方法,例如将json存储在服务器中的文件中,然后使用ajax为页面提供服务,从而查询页面.

Or , should i use the same method , like store the json in a file in the server , then serve the page with ajax which will query the page.

请帮助我.这是我写的代码,

please help me. here is the code which i wrote ,

app-server.js(主文件):

app-server.js(the main file):

// import express module
var express = require('express');
var bodyParser = require('body-parser');
var app = express();

// define all required template files to be served and also define the template engine
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');

// Useful modules
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

// import the routes
require('./router')(app);
app.listen(8080);

router.js:

router.js:

var crypto = require('crypto');
var express = require('express');

module.exports = function (app) {

    // define the static routes.
    app.use('/static', express.static('./static'));
    app.use('/media', express.static('./media'));

    //defining the controller.
    var parserlib = require('./controller.js')

    // Define the home root path
    app.get('/', function (req, res) {
        // shows the home search page.
        res.render('index', {content:'template success'});
    });

    app.get('/search', function(req, res){
        res.redirect('/');
    });

    app.post('/search', parserlib.parserlib);
}

controller.js:

controller.js:

var crypto = require('crypto');
var path = require('path')
var childProcess = require('child_process')

exports.parserlib= function(req, res){

    var output = '';
    var url = req.body.search_url;

    var childArgs = [
     path.join(__dirname, 'external-script.js'),
     url,
    ]

    // execute the script in a separate thread.
    childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
        // handle results
        console.log(stdout);
        output = stdout;
        //console.log(err);
        //res.send(output);
    });
    //res.send(output);
};

所以,我想看到的是,首先向客户端发送响应,说明其正在加载,然后我要使用处理后的数据来更新.在其他语言中,不可能发送多个响应.不确定nodejs.

so , what i want to see is, first send a response to client stating that its loading, then i want to update the with processed data. In other languages its not possible to send multiple responses. Not sure about nodejs.

此外,我是否必须将已处理的lib的json输出存储到文件中,然后使用ajax进行查询?还是可以直接将json对象更新到客户端?

Also, do i have to store the json output from the processed lib to a file and then use ajax to query ? or is it possible to directly update the json object to the client ?

谢谢

推荐答案

这不是HTTP的工作方式.客户不会期望的.这与Node或任何其他框架无关.尝试执行操作的方法是实际发送事物正在加载的响应,然后具有其他报告状态的机制.

This is just not how HTTP works. The clients won't expect it. This has nothing to do with Node or any other framework. The way to do what you're attempting is to actually send a response that the thing is loading, and then have some other mechanism for reporting state.

作为示例,您可以设计一个RESTful API.在该RESTful API中,您可以定义用于创建新事物的端点:

As an example, you might design a RESTful API. In that RESTful API you might define a endpoint for creating new things:

POST/api/things

POST /api/things

客户端将向其发布数据以创建新事物.响应应该是提供新创建资源位置的信息,例如HTTP 301到/api/things/1.

The client would post data to that to create a new thing. The response should be something that provides a location of the newly created resource, for example an HTTP 301 to /api/things/1.

如果用户转到/api/things/1并且尚未完成操作,则可以将临时重定向(303)重定向到/api/things/1/status,它提供了一些有用的状态信息,或者仅发出404.

If the user goes to /api/things/1 and the thing isn't done getting made yet, then you can either do a temporary redirect (303) to /api/things/1/status which provides some helpful status information, or just issue a 404.

如果您实际上想发回服务器端的状态信息推送,那么您应该查看WebSockets或某种纯套接字API,两者都不是Express提供的,但是两者都可以在Node中使用. (签出socket.io库和net核心库)

If you actually want to send back server-side pushes of status information, then you should be looking at WebSockets or a pure Socket API of some kind, neither of which is provided by Express, but both of which are available in Node (checkout the socket.io library and the net core library)

这篇关于Node.js,Express.js-如何提供延迟响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆