4

Pretty much purely for pedagogical purposes, I'm serving both my front and back end data out of my one node server. Right now, I'm at the point where I've received my client request successfully, created some data based on said request, am able to console log it, etc. Everything is fine up to that point. My issue is that in the event that my data is only an html file, which is being read with the fs library, it will not render on the page when I attempt to serve it out in my res.end() or res.write(). I can see it's exactly what I want and expect when I console log it, but it just doesn't render in the browser. Any help would be appreciated. I've got it set up to where I'm handling my requests in an "if/else" wherein I only have the two scenarios of "/" (home), in which case I serve the html file, and anything else because the server really only needs to handle those two events. Thanks in advance.

Edit. This is what I have so far:

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 last case, is just my HTML file. It just won't render in the page.

1
  • I just edited my answer to match with your code, see if it can help. :) Commented Jun 25, 2015 at 19:13

2 Answers 2

5

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

I just made a small test here, and this 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);

hello-world.html

<h3>Hello World</h3>

Edit: To match with your code, try this:

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Serving html in asynchronous way works something like that;

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.