简体   繁体   中英

How to send plain HTML from Express.js

My index.html file has this:

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

I want that {{qwe}} to be sent literally, ie not replaced by variables populated by the server.

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:

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?

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.

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?

In Express when server will get a GET request of / it will search for /public/index.html serve that as response. You don't have to add router for that / .

So you can just put your index.html in public folder and hit / from browser. you should get it.

I think the code you pasted is OK. It will send the file raw content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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