简体   繁体   English

Nodejs,expressjs - 如何提供延迟响应

[英]Nodejs, expressjs - how to serve delayed response

I am building a webservice, for which i am using nodejs, phantomjs and expressjs.我正在构建一个网络服务,为此我使用了 nodejs、phantomjs 和 expressjs。 I am learning all the three.我正在学习所有三个。

I want to serve a delayed response to the clients after processing their query.我想在处理他们的查询后为客户提供延迟响应。 Like for example,例如,

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.我正在处理来自我的客户的某些输入,然后,我想在后端处理数据,平均需要大约 10 秒。 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.是否可以在节点中对同一请求或延迟响应发送多个响应,以便模板自动更新内容。

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.或者,我应该使用相同的方法,例如将 json 存储在服务器中的文件中,然后使用 ajax 提供页面以查询页面。

please help me.请帮我。 here is the code which i wrote ,这是我写的代码,

app-server.js(the main file): app-server.js(主文件):

// 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:路由器.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:控制器.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);
};

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.不确定nodejs。

Also, do i have to store the json output from the processed lib to a file and then use ajax to query ?另外,我是否必须将处理后的 lib 的 json 输出存储到文件中,然后使用 ajax 进行查询? or is it possible to directly update the json object to the client ?或者是否可以直接将 json 对象更新到客户端?

Thanks谢谢

This is just not how HTTP works.这不是 HTTP 的工作方式。 The clients won't expect it.客户不会期望它。 This has nothing to do with Node or any other framework.这与 Node 或任何其他框架无关。 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.做你正在尝试的方法是实际发送一个正在加载的响应,然后有一些其他的报告状态的机制。

As an example, you might design a RESTful API.例如,您可以设计一个 RESTful API。 In that RESTful API you might define a endpoint for creating new things:在该 RESTful API 中,您可以定义用于创建新事物的端点:

POST /api/things POST /api/东西

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 .响应应该是提供新创建资源位置的内容,例如 HTTP 301 到/api/things/1

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.如果用户转到/api/things/1并且事情还没有完成,那么你可以做一个临时重定向(303)到/api/things/1/status提供一些有用的状态信息,或者只需发出 404。

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)如果您真的想发回服务器端的状态信息推送,那么您应该查看 WebSockets 或某种纯 Socket API,这两种 API 都不是 Express 提供的,但两者都可以在 Node 中使用(请查看socket.io库和net核心库)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM