DEV Community

Discussion on: Node.js server fails to render pages

Collapse
 
sfiquet profile image
Sylvie Fiquet

I'm not familiar with Typescript but with vanilla Node you'd normally get the function stack when you have an error, which would help to pinpoint which line of your code causes the error. You have two calls to fs.createReadStream, are you sure which one is called? I would check the value of pathToIndexView and sendToErrorPage and maybe add console.log before and after each createReadStream to be sure of what is going on.

Collapse
 
dwmedina profile image
Daniel Medina • Edited

Very good call on adding the additional console.log's!

I changed my server code to no longer use res.end() as
I learned that this is automatically taken care of by the .pipe() method.

The server looks like this now:

const app: http.Server = http
    .createServer((req: http.IncomingMessage, res: http.ServerResponse) => {

        res.writeHead(httpOkStatus, {
            "Content-Type": "text/html"
        });

        const pathToIndexView: string = routeMap[req.url as string];
        const sendToErrorPage: boolean = !routeMap[req.url as string];

        if (sendToErrorPage) {
            res.writeHead(httpNotFoundStatus, {
                "Content-Type": "text/html"
            });
            console.log("Path to error view: \n" + "\t" + path.join(__dirname, "..", "views", "404.html") + "\n");
            let errorPageFileStream: fs.ReadStream = fs.createReadStream(path.join(__dirname, "..", "views", "404.html") as string, "utf8");
            errorPageFileStream.pipe(res);
        }
        console.log("Request url: " + req.url + "\nTime:" + Date.now());
        console.log("Path to index view: \n" + "\t" + pathToIndexView + "\n");
        const src: fs.ReadStream = fs.createReadStream(pathToIndexView, "utf8");

        src.pipe(res);
    });

When I start the server and make a request to the home page, these are the results:

The server has started and is listening on port number: 3000
Request url: /
Time:1580704003783
Path to index view:
        C:\Users\Daniel\Documents\TypeScript\serve_html\views\index.html

Request url: /favicon.ico
Time:1580704004292
Path to index view: 
        C:\Users\Daniel\Documents\TypeScript\serve_html\favicon.ico

The path to the index view is changing with each request.
Additionally, if I try to have my 404 page appear,
I receive the following,

Request url: /error
Time:1580704087558
Path to index view: 
        undefined

When the path to the index changes, it attempts to find something that isn't there and becomes undefined.

I feel that I'm a lot closer than I was before. I'll keep logging and stepping through to see what's causing this.

Collapse
 
sfiquet profile image
Sylvie Fiquet

Two things:

  • looks like it's not stepping inside the if (sendToErrorPage) block so sendToErrorPage is not set to true.
  • shouldn't the second createReadStream be wrapped in an else? It's only supposed to be called when the path is correct, right?
Thread Thread
 
dwmedina profile image
Daniel Medina

Wrapping the second createReadStream inside an else got everything working.

For good measure, I added the favicon tags in both HTML <head> tags.

Thank you so much for your help!! 🎉🎉