我正在尝试用联合报告创建我的第一份报告。我已经跟踪了文档,但是我无法生成最简单的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/,什么都不会发生。我还以为会有个“你好世界”的网页呢。
在服务器端,我在控制台上看到:
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我遗漏了什么?
我在Ubuntu16.04上使用node.js v8.1.2。
发布于 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),而不仅仅是一个请求。/favicon.ico、/robots.txt的请求)调用/favicon.ico,这在浏览器中是不好的,因为正如我已经解释过的那样,您的浏览器对单个页面负载发出3次请求。jsreport.render,这意味着当您的第一个请求到达时,jsreport将尝试初始化自己,因为上面解释了问题(在您的http服务器中没有正确的路由),这导致jsreport在第一个页面加载时尝试初始化3次,这将导致一个没有错误消息的异常退出您的进程(我们将更新一些东西,以便在将来更好地处理此异常)。最后,这里有一些代码完成了hello测试用例(有些代码过滤了一些不必要的请求,比如/robots.txt、/favicon.ico,但是在生产代码中,您应该在您的http服务器中实现一个正确的路由器逻辑。如果您不想自己编写代码,只需使用类似于快递的代码即可)
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');https://stackoverflow.com/questions/48840565
复制相似问题