Serving HTML file from Node server

To a large extent, solely for educational purposes, I serve both my data and the data from my node server. Right now, I am at the point where I received my client request successfully, created some data based on the specified request, I can register it in a console, etc. Everything is fine up to this point. My problem is that if my data is only an html file that is read in the fs library, it will not appear on the page when I try to execute it in my res.end () or res.write () file . I can see this exactly what I want and expect it to console me, but it just doesn’t appear in the browser. Any help would be greatly appreciated. I set it up to where I process my requests in "if / else", where I have only two "/" scripts(home), in which case I am serving the html file and something else, because the server really only needs to handle these two events. Thanks in advance.

Change This is what I still have:

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();
}

I can see the data in the console, which in the latter case is only my HTML file. It simply will not appear on the page.

+4
source share
2 answers

How did you try to serve html with res.end()and res.write()?

I just did a little test here and it works:

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);

Hi-world.html

<h3>Hello World</h3>

Edit: to match your code, try the following:

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;
    }
}
+4
source

Serving html in asynchronous mode works something like this:

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');
Run codeHide result
0
source

All Articles