jsreport你好世界

时间:2018-02-17 10:58:56

标签: node.js jsreport

我正在尝试使用jsreport创建我的第一份报告。我跟着documentation,但我无法生成最简单的Hello世界。

我试过了:

npm install jsreport

然后创建一个简单的服务器:

var http = require('http');
var jsreport = require('jsreport');

http.createServer(function (req, res) {

    jsreport.render("<h1>Hello world</h1>").then(function(out) {
        out.stream.pipe(res);
    }).catch(function(e) {
        res.end(e.message);
    });

}).listen(1337, '127.0.0.1');

服务器在端口1337上运行。

但如果我试图打开http://localhost:1337/则没有任何反应。我期待一个包含Hello world的页面。

在服务器端,我进入控制台:

2018-02-17T10:55:16.009Z - info: Initializing jsreport@1.10.0 in development mode using configuration file: jsreport.config.json
2018-02-17T10:55:16.011Z - info: Setting process based strategy for rendering. Please visit http://jsreport.net/learn/configuration for information how to get more performance.
2018-02-17T10:55:16.013Z - info: Searching for available extensions in /home/jgr/WebstormProjects/GeoMasterBoard/server/
2018-02-17T10:55:16.016Z - info: Extensions location cache not found, crawling directories

我是否需要运行jsreport服务器或此代码应该足够?

我还尝试按照documentation安装jsreport服务器。

在控制台上显示jsreport start之后:

2018-02-17T10:42:46.013Z - info: Initializing jsreport@1.10.0 in development mode using configuration file: jsreport.config.json
2018-02-17T10:42:46.015Z - info: Setting process based strategy for rendering. Please visit http://jsreport.net/learn/configuration for information how to get more performance.
2018-02-17T10:42:46.023Z - info: Searching for available extensions in /home/jgr/WebstormProjects/GeoMasterBoard/server/
2018-02-17T10:42:46.025Z - info: Extensions location cache not found, crawling directories

但是当我尝试打开http://localhost:5488/时没有任何反应。如果我这样做:nmap -p 5488 localhost芒果是:

PORT     STATE  SERVICE
5488/tcp closed unknown

我错过了什么?

我在Ubuntu 16.04上使用node.js v8.1.2。

1 个答案:

答案 0 :(得分:0)

您的代码无效,原因如下:

  • 当您在http://localhost:1337/打开浏览器时,您的浏览器实际上会发出3个不同的请求(1&gt; http://localhost:1337/,2&gt; http://localhost:1337/favicon.ico,3&gt; {{1 }),而不仅仅是一个
  • 您用来处理http服务器的代码没有正确路由,它应该只处理一次网址,现在它只是在通过您的服务器的每个请求上调用http://localhost:1337/robots.txt(甚至是jsreport.render/favicon.ico),这在浏览器中也很糟糕,因为正如我已经解释过的那样,你的浏览器就像单个页面加载请求一样。
  • 您正在请求处理中使用快捷方式/robots.txt,这意味着当您的第一个请求到达时,jsreport将尝试初始化自己,因为上面解释了问题(没有在您的http中进行正确的路由服务器)这导致jsreport尝试在您的第一个页面加载时初始化3次,这导致一个未捕获的异常退出您的进程而没有错误消息(我们将更新一些内容以便将来更好地处理此异常)。

最后,这里有一些代码可以完成你的hello world测试用例(有些代码可以过滤不需要的请求,比如jsreport.render/robots.txt,但是在生产代码中,你应该在你的代码中实现一个合适的路由器逻辑http服务器。如果您不想自己编码,只需使用类似express

的内容
/favicon.ico
相关问题