【问题标题】:Serving HTML file from Node server从节点服务器提供 HTML 文件
【发布时间】:2015-09-12 09:43:22
【问题描述】:

几乎完全出于教学目的,我从我的一个节点服务器提供前端和后端数据。现在,我已经成功收到了我的客户请求,根据所述请求创建了一些数据,能够控制台记录它等等。到目前为止一切都很好。我的问题是,如果我的数据只是一个使用 fs 库读取的 html 文件,当我尝试在 res.end() 或 res.write 中提供它时,它不会呈现在页面上()。当我控制台记录它时,我可以看到它正是我想要和期望的,但它只是没有在浏览器中呈现。任何帮助,将不胜感激。我已经将它设置为我在“if/else”中处理我的请求的位置,其中我只有“/”(home)两个场景,在这种情况下我提供 html 文件,以及其他任何内容,因为服务器实际上只需要处理这两个事件。提前致谢。

编辑。这是我目前所拥有的:

function responseHandler(req, res) {
 res.writeHead(200, {"Content-Type": "text/html"});
 if (req.url.match("fav")) {
   res.end("");
   return;
 }
 else if (req.url.match("/endpoint")) {
   var input = req.url.match(/endpoint\/(.*)/)[1];
   var output = endpoint.toHTML(decodeURI(input));
   res.end(data);
   console.log(input, req.url)
 }
 else {
   fs.readFile("index.html", "utf8", function(err, data) {
     console.log("data:" + data);
     var input = req.url.match(/endpoint\/(.*)/)[1];
     var output = endpoint.toHTML(decodeURI(input));
   });
 }

 res.end();
}

我可以在控制台中看到数据,在最后一种情况下,它只是我的 HTML 文件。它只是不会在页面中呈现。

【问题讨论】:

  • 我刚刚编辑了我的答案以匹配你的代码,看看它是否有帮助。 :)

标签: html node.js file server


【解决方案1】:

以异步方式提供 html 的工作方式类似;

var fs = require('fs');
var http = require('http');

http.createServer(function(req, res){
  res.writeHead(200, {'Content-Type': 'text/html'});
  fs.readFile('index.html', function(err, data){
    if(err){
      return console.log(err);
    }
  res.end(data);
  });
}).listen(8080);
console.log('Server is running on Port: 8080');

【讨论】:

    【解决方案2】:

    您是如何尝试使用 res.end() 和 res.write() 提供 html 的?

    我只是在这里做了一个小测试,这很有效:

    app.js

    var http = require('http');
    var fs = require('fs');
    
    var html = fs.readFileSync('hello-world.html');
    
    http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(html);
    }).listen(8000);
    

    hello-world.html

    <h3>Hello World</h3>
    

    编辑:要与您的代码匹配,请尝试以下操作:

    function responseHandler(req, res) {
        res.writeHead(200, {"Content-Type": "text/html"});
    
        if (req.url.match("fav")) {
            res.end("");
            return;
        } else if (req.url.match("/endpoint")) {
            var input = req.url.match(/endpoint\/(.*)/)[1];
            var output = endpoint.toHTML(decodeURI(input));
    
            console.log(input, req.url);
    
            // we have no data variable in this scope
            res.end("");
    
            // I added a return statement in each step
            // Just to be clear that we don't want to go if any
            // condition have fit, since we cannot call res.end()
            // more than once
            return;
        } else {
            fs.readFile("index.html", "utf8", function(err, data) {
                // error handling
                if (err) return res.end(err);
    
                // now we have the data
                console.log("data:" + data);
                res.end(data);
            });
    
            return;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-05
      • 2017-05-22
      • 2018-03-10
      • 2013-03-25
      • 2019-01-13
      • 2018-05-14
      • 1970-01-01
      • 2012-06-12
      相关资源
      最近更新 更多