node.js - jsreport Hello World

标签 node.js jsreport

我正在尝试使用 jsreport 创建我的第一份报告。我已经关注了documentation ,但我无法生成最简单的 Hello world。

我已经尝试过:

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 awnser 是:

PORT     STATE  SERVICE
5488/tcp closed unknown

我错过了什么?

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

最佳答案

由于以下原因,您的代码无法运行:

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

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

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

http.createServer(function(req, res) {
  if (req.url !== '/') {
    return console.log('ignoring request:', req.url);
  }

  console.log('request:', req.url);

  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');

关于node.js - jsreport Hello World ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48840565/

相关文章:

javascript - 如何在 jsreport 中包含助手

javascript - Azure Function App jsreport on HTTP 触发器不起作用

javascript - 从 REST API 获取数据到 jsreports

mysql - 当有新的 mysql 记录插入表中时创建 Node.js 事件监听器(用于通知提要)

javascript - 用于生产的Node.js中的错误处理和转发

node.js - Mongodb 插入对象值

javascript - Telegram 机器人内联键盘标记回调用于 channel 消息

javascript - 如何使用 jsreport 和 jsreport Chrome Pdf 渲染 1k pdf 文件?

node.js - jsreport 在 Node 10.15 中无法使用 html 到 xlsx 配方

javascript - 404 未找到从 nodejs/connect.static 获取静态文件 ('../angularjs' )