nodejs 向客户端发送 html 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20345936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:25:04  来源:igfitidea点击:

nodejs send html file to client

node.jsexpresshtml-rendering

提问by user3044147

I use this function to send html file to client, but in client I get nothing (blank page) without error. Something I wrong?, please help?

我使用此函数将 html 文件发送到客户端,但在客户端我什么也没有(空白页)没有错误。我有什么问题吗?请帮忙?

var express = require('express'); 
var fs = require('fs');
var app = express();
app.set('view engine', 'jade');
app.engine('jade', require('jade').__express); 
    app.get('/test', function(req, res) {
            fs.readFile(__dirname + '/views/test.html', 'utf8', function(err, text){
                res.send(text);
            });
var port = process.env.PORT || 80;
var server = app.listen(port);
console.log('Express app started on port ' + port);

My test.html file

我的 test.html 文件

<!DOCTYPE html>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <style something here </style>
      <title>Test</title>
      <script src="..."></script>
   </head>
<body>
    <div> Somthing here </div>

    <script type="text/javascript">
        //something here
    </script>
</body></html>

回答by Tim Brown

Try your code like this:

像这样尝试你的代码:

var app = express();
app.get('/test', function(req, res) {
    res.sendFile('views/test.html', {root: __dirname })
});
  1. Use res.sendFileinstead of reading the file manually so express can handle setting the content-type properly for you.

  2. You don't need the app.engineline, as that is handled internally by express.

  1. 使用res.sendFile而不是手动读取文件,因此 express 可以为您正确设置内容类型。

  2. 您不需要该app.engine线路,因为它是由 express 在内部处理的。

回答by efkan

After years, I want to add another approach by using a viewengine in Express.js

多年后,我想通过使用视图引擎添加另一种方法Express.js

var fs = require('fs');

app.get('/test', function(req, res, next) {
    var html = fs.readFileSync('./html/test.html', 'utf8')
    res.render('test', { html: html })
    // or res.send(html)
})

Then, do that in your views/testif you choose res.rendermethod at the above code (I'm writing in EJS format):

然后,views/test如果您res.render在上面的代码中选择方法(我用 EJS 格式编写),请执行此操作:

<%- locals.html %>

That's all.

就这样。

In this way, you don't need to break your View Engine arrangements.

这样,您就不需要打破您的视图引擎安排。

回答by Adiii

you can render page in express more easily

您可以更轻松地在 express 中渲染页面

var app   = require('express')();        //to install express write "npm install express"

app.get('/signup',function(req,res){

 res.sendFile(path.join(__dirname+'/signup.html'));

});

});

so if u request like http://127.0.0.1:8080/signupthat it will render signup.html page

所以如果你这样请求http://127.0.0.1:8080/signup它会呈现 signup.html 页面