首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jsreport你好世界

jsreport你好世界
EN

Stack Overflow用户
提问于 2018-02-17 10:58:55
回答 1查看 754关注 0票数 2

我正在尝试用联合报告创建我的第一份报告。我已经跟踪了文档,但是我无法生成最简单的Hello。

我试过:

代码语言:javascript
复制
npm install jsreport

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

代码语言:javascript
复制
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/,什么都不会发生。我还以为会有个“你好世界”的网页呢。

在服务器端,我在控制台上看到:

代码语言:javascript
复制
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之后,它将显示在控制台上:

代码语言:javascript
复制
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是:

代码语言:javascript
复制
PORT     STATE  SERVICE
5488/tcp closed unknown

我遗漏了什么?

我在Ubuntu16.04上使用node.js v8.1.2。

EN

回答 1

Stack Overflow用户

发布于 2018-02-18 18:24:36

您的代码无法工作,原因如下:

  • 当您在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的请求)调用/favicon.ico,这在浏览器中是不好的,因为正如我已经解释过的那样,您的浏览器对单个页面负载发出3次请求。
  • 在请求处理中,您正在使用快捷的jsreport.render,这意味着当您的第一个请求到达时,jsreport将尝试初始化自己,因为上面解释了问题(在您的http服务器中没有正确的路由),这导致jsreport在第一个页面加载时尝试初始化3次,这将导致一个没有错误消息的异常退出您的进程(我们将更新一些东西,以便在将来更好地处理此异常)。

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

代码语言:javascript
复制
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');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48840565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档