Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 管道化响应时无法嵌入EJS_Node.js_Express_Stream_Response - Fatal编程技术网

Node.js 管道化响应时无法嵌入EJS

Node.js 管道化响应时无法嵌入EJS,node.js,express,stream,response,Node.js,Express,Stream,Response,所以我想我用pipe一个html文件作为响应,而不是res.render来获得更快的性能 而不是 res.render('form',{title:'Login',userField:'Username',passField:'Password',photo: photo}); 我知道 在form.ejs中,我有一个在使用管道进行响应时不起作用。在我的浏览器中,我看到 我该如何解决这个问题?或者这是因为管道逐段发送html文件,因此无法正确渲染和嵌入,因此没有解决方案 谢谢stream.pip

所以我想我用
pipe
一个html文件作为响应,而不是
res.render
来获得更快的性能

而不是

res.render('form',{title:'Login',userField:'Username',passField:'Password',photo: photo});
我知道

form.ejs
中,我有一个
在使用
管道进行响应时不起作用。在我的浏览器中,我看到

我该如何解决这个问题?或者这是因为管道逐段发送html文件,因此无法正确渲染和嵌入,因此没有解决方案

谢谢

stream.pipe()
方法完全不知道您的任何渲染需求

res.render()
的设计目的是希望得到一个可渲染的模板和一个已配置的视图引擎。您可以在express的源代码中看到:

打哪一个电话

当您将结果传递给
stream.pipe
时,绝对不会有任何东西试图以
res.render
的方式呈现属性

仅仅通过使用管道,您也可能无法获得任何“性能”

简而言之:您希望渲染结果,因此使用
res.render()

方法
stream.pipe()
完全不知道您的任何渲染需求

res.render()
的设计目的是希望得到一个可渲染的模板和一个已配置的视图引擎。您可以在express的源代码中看到:

打哪一个电话

当您将结果传递给
stream.pipe
时,绝对不会有任何东西试图以
res.render
的方式呈现属性

仅仅通过使用管道,您也可能无法获得任何“性能”


简言之:您希望呈现结果,因此使用
res.render()

只需添加到上面的答案中即可。。。在特定视图需要管道的情况下。 正如duncanhall所说,通过调用
res.render('view')
,使用“.ejs”模板引擎是让“ejs标记”工作的唯一方法。另外,正如您可能在代码中指定的,您的视图文件夹将用于ejs,它将尝试挂接该文件夹中的所有视图,以便您不能将同一文件夹用于html文件,否则它将崩溃:

Error: Failed to lookup view "clubs" in views directory "...
因此,如果您只使用带有css的html以及纯静态页面的一些JS和图片,您可以创建一个特殊的html文件夹,html文件将放在其中。 然后使用:

app.get('/', function(req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    var clubs = fs.createReadStream(__dirname + '/html/clubs.html')
    clubs.pipe(res);
});
例如,是的,如果该路由被大量访问,那么它将提高IO性能,因为块将以一种触发式的方式被攻击! 毕竟,非阻塞IO、流、长轮询和异步是使节点高效的因素。
干杯~

只是为了补充上面的答案。。。在特定视图需要管道的情况下。 正如duncanhall所说,通过调用
res.render('view')
,使用“.ejs”模板引擎是让“ejs标记”工作的唯一方法。另外,正如您可能在代码中指定的,您的视图文件夹将用于ejs,它将尝试挂接该文件夹中的所有视图,以便您不能将同一文件夹用于html文件,否则它将崩溃:

Error: Failed to lookup view "clubs" in views directory "...
因此,如果您只使用带有css的html以及纯静态页面的一些JS和图片,您可以创建一个特殊的html文件夹,html文件将放在其中。 然后使用:

app.get('/', function(req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    var clubs = fs.createReadStream(__dirname + '/html/clubs.html')
    clubs.pipe(res);
});
例如,是的,如果该路由被大量访问,那么它将提高IO性能,因为块将以一种触发式的方式被攻击! 毕竟,非阻塞IO、流、长轮询和异步是使节点高效的因素。
干杯~

直接从
fs
加载模板不是一个好主意,也许您可以在收听之前将其加载到内存中。例如:

const viewFoo = fs.readFileSync('./views/foo.html');
const templateFoo = ejs.compile(viewFoo);

// then in your handler:
res.end(templateFoo({ title:'Login' /* ... */ }));
const { compile } = require('ejs-stream2');

const template = compile('<div><%- contentStream %-></div>');

app.get('/', (req, res) => {
  const stream = template({ contentStream: renderToNodeStream(<App/>);
  stream.pipe(res);
})
如果要使用流作为数据在
ejs
模板中渲染, 你可以试试。 例如:

const viewFoo = fs.readFileSync('./views/foo.html');
const templateFoo = ejs.compile(viewFoo);

// then in your handler:
res.end(templateFoo({ title:'Login' /* ... */ }));
const { compile } = require('ejs-stream2');

const template = compile('<div><%- contentStream %-></div>');

app.get('/', (req, res) => {
  const stream = template({ contentStream: renderToNodeStream(<App/>);
  stream.pipe(res);
})
const{compile}=require('ejs-stream2');
常量模板=编译(“”);
应用程序获取(“/”,(请求,请求)=>{
const stream=template({contentStream:renderToNodeStream();
流管(res);
})

直接从
fs
加载模板不是一个好主意,也许您可以在侦听之前将其加载到内存中。例如:

const viewFoo = fs.readFileSync('./views/foo.html');
const templateFoo = ejs.compile(viewFoo);

// then in your handler:
res.end(templateFoo({ title:'Login' /* ... */ }));
const { compile } = require('ejs-stream2');

const template = compile('<div><%- contentStream %-></div>');

app.get('/', (req, res) => {
  const stream = template({ contentStream: renderToNodeStream(<App/>);
  stream.pipe(res);
})
如果要使用流作为数据在
ejs
模板中渲染, 你可以试试。 例如:

const viewFoo = fs.readFileSync('./views/foo.html');
const templateFoo = ejs.compile(viewFoo);

// then in your handler:
res.end(templateFoo({ title:'Login' /* ... */ }));
const { compile } = require('ejs-stream2');

const template = compile('<div><%- contentStream %-></div>');

app.get('/', (req, res) => {
  const stream = template({ contentStream: renderToNodeStream(<App/>);
  stream.pipe(res);
})
const{compile}=require('ejs-stream2');
常量模板=编译(“”);
应用程序获取(“/”,(请求,请求)=>{
const stream=template({contentStream:renderToNodeStream();
流管(res);
})

这一点很好。我不认为管道是模板和视图不可知的。Render设计用于处理模板和EJB之类的事情。我想管道最适合用于JSON和数据文件。谢谢。这一点很好。我不认为管道是模板和视图不可知的。Render设计用于处理模板和EJB之类的事情。管道是我想最好用于JSON和数据文件。谢谢。