javascript - Express.JS中 'app.post'之后如何发送回客户端

标签 javascript express

我目前正在开发一个部署管道,可以为在 chrome 扩展中运行的文本摘要深度学习模型提供服务,该模型总结了浏览器中突出显示的文本 block 。

我的简单前台如下所示,用纯 JavaScript 编写

chrome.tabs.executeScript( {
    code: "window.getSelection().toString();"
}, function(selection) {

    document.write(selection[0]);

    var post =
        '<form action="http://localhost:8080/client_txt" method="POST" id="hlgt_form">' +
        '<input type="hidden" id="hlgt" name="hlgt" value="">' +
        '</form>';

    document.write(post);

    document.getElementById('hlgt').value = selection[0];
    // it stores highlights into value of <input>

    document.getElementById('hlgt_form').submit();
});

我的 Express.JS 服务器如下所示

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

const python = require('python-shell');

// define conda specific python
const python_path = '/Users/****/anaconda3/envs/****/bin/python';

// define sysArgs for python script
const mode = "decode";
const data_path = "/Users/****/Downloads/finished_files/chunked/test_000.bin";
const vocab_path = "/Users/****/Downloads/finished_files/vocab";
const log_root = "/Users/****/Downloads";
const exp_name = "pretrained_model_tf1.2.1";


app.use(bodyParser.urlencoded({ extended: true }));

app.post('/client_txt', (req, ) => {

    const text = `${req.body.hlgt}`;

    console.log(text);

    /* python runs in express js */
    const options = {
        mode: 'text',
        pythonPath: python_path,
        pythonOptions: ['-u'],
        scriptPath: '/Users/****/project/text-summarizer/',

        args: [ '--hlgt', text,
                '--mode', mode,
                '--data_path', data_path,
                '--vocab_path', vocab_path,
                '--log_root', log_root,
                '--exp_name', exp_name,
                '--max_enc_steps', 400,
                '--max_dec_steps', 120,
                '--coverage', 1,
                '--single_pass', 1,
                '--batch_size', 1,
                '--beam_size', 1]

    };
    //TODO: Get return val from python script not print val
    python.PythonShell.run('run_summarization.py',
        options,
        function (err, results) {
        if (err)
            throw err;
        console.log("\n ## summary ##\n" +
            results[results.length-1] + "\n"); // python "print" val stored in results
    });


    /******************************/

});

const port = 8080;

app.listen(port, () => {
    console.log(`Server running on port: ${port}`);
});

服务器端从前面突出显示的文本中获取发布的文本,并将其传递给深度学习Python代码,文本为sysarg

结果有一个python终端打印输出,最后一个是总结的文本字符串。

我想将结果发送回客户端。

我应该添加什么?当我继续使用 post 方法时可以吗?

最佳答案

您只需添加响应部分即可。

app.post('/client_txt', (req, ) => {

更改为

app.post('/client_txt', (req, res) => {
  // After all your processing and getting of the result.
  res.send(results);
}

关于javascript - Express.JS中 'app.post'之后如何发送回客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56744559/

相关文章:

mysql - Bookshelf(knex) - belongsToMany 关系不起作用

node.js - Express-handlebars 找不到目录或文件

javascript - 添加 href URL 字符串

javascript - 使用 React Suspense 和 React.lazy 子组件进行 Jest/Enzyme 类组件测试

javascript - 找不到 Qt Creator 导入文件

node.js - Bootstrap渲染空白白页-node js-express-jade

node.js - 关闭连接而不是设置 'keep-alive'

javascript - 将从 ajax 请求返回的 html 转换为对象会导致奇怪的并发症

javascript - 为什么我们在 angularjs 中两次注入(inject)我们的依赖项?

postgresql - 属性无法在 Sequelize Model Create 中正确插入