jsreport Hello world



我正在尝试使用JSReport创建我的第一个报告。我已经遵循了文档,但是我无法生成最简单的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服务器还是应该足够?

我还尝试在文档之后安装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

我缺少什么?

我正在使用node.js v8.1.2,在ubuntu 16.04。

您的代码不起作用,因为以下原因:

  • 当您在http://localhost:1337/打开浏览器时,您的浏览器实际上是在提出3种不同的请求(1-> http://localhost:1337/,2-> http://localhost:1337/favicon.ico,3-> http://localhost:1337/robots.txt(,而不仅仅是一个
  • 您用来处理HTTP服务器的代码没有进行适当的路由,它应该仅处理一次URL,现在它只是在通过您的服务器的每个请求中调用jsreport.render(甚至是用于/favicon.ico/robots.txt(,这在浏览器中很糟糕,因为正如我已经解释的那样
  • 您在请求处理中使用快捷键jsreport.render,这意味着当您的第一个请求到达时,JSReport将尝试初始化自身,因为上述问题(不在HTTP服务器中进行适当的路由(此结果在JSReport试图在首页加载中初始化3次,这导致未经错误消息离开您的流程的例外(我们将更新一些内容以更好地处理此例外(。

最后,这里有一些代码可以完成您的Hello World测试用例(一些代码,可以过滤/robots.txt/favicon.ico等不需要的请求,但是在生产代码中,您应该在HTTP Server中实现适当的路由器逻辑。如果您不在想要自己编码,只需使用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');

最新更新