简体   繁体   English

如何从Express.js发送纯HTML

[英]How to send plain HTML from Express.js

My index.html file has this: 我的index.html文件具有以下内容:

<input name='qwe'>
{{qwe}}

I want that {{qwe}} to be sent literally, ie not replaced by variables populated by the server. 我希望按{{qwe}}发送{{qwe}} ,即不要被服务器填充的变量替换。

How should I do that? 我应该怎么做? I was thinking in this pseudo code: 我在想这个伪代码:

app.get('*',function(req,res){
  fs.read('index.html',function(data){
    res.send(data)
  })
})

But I suppose I don't know how is this usually done. 但是我想我不知道这通常是怎么做的。 Maybe I have to use a predefined Express.js template engine: 也许我必须使用预定义的Express.js模板引擎:

app.engine('html', ...)

But I don't know what part of the documentation to read. 但我不知道该阅读文档的哪一部分。 I also took a look at consolidate.js , but all of these templates, seem to be, "template engines", do I really need a template engine? 我还查看了consolidate.js ,但是所有这些模板似乎都是“模板引擎”,我真的需要一个模板引擎吗?

I want to send a file as it is. 我想按原样发送文件。

By the way, I am doing all of this because I want to work with Angular.js, but for experimentation purposes I am not using MEAN , I assume MEAN has to do something like this. 顺便说一下,我之所以这样做,是因为我想使用Angular.js,但是出于实验目的,我没有使用MEAN ,我认为MEAN必须做这样的事情。

Update 更新

I am trying this: 我正在尝试:

app.get('*',function(req, res){
  require('fs').readFile('views/index.html', 'utf8', function(err, data){
    if(err) throw err;
    res.send(data);
  })
  // res.sendfile('views/index.html');
});

But file is downloaded and not rendered, any advice? 但是文件已下载但未呈现,有什么建议吗? I suppose I have to set Content-Type or another header? 我想我必须设置Content-Type或另一个标题吗?

In Express when server will get a GET request of / it will search for /public/index.html serve that as response. 在Express中,服务器将获得/GET请求,它将搜索/public/index.html作为响应。 You don't have to add router for that / . 您不必添加路由器为/

So you can just put your index.html in public folder and hit / from browser. 所以,你可以把你index.htmlpublic文件夹和命中/从浏览器。 you should get it. 你应该得到它。

I think the code you pasted is OK. 我认为您粘贴的代码还可以。 It will send the file raw content. 它将发送文件原始内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM