I am trying out node and it’s Express framework via the Express boilerplate installation. It took me a while to figure out I need Redis installed (btw, if you’re making a boilerplate either include all required software with it or warn about the requirement for certain software – Redis was never mentioned as required) and to get my way around the server.js file.
Right now I’m still a stranger to how I could build a site in this..
There is one problem that bugs me specifically – when I run the server.js file, it says it’s all good. When I try to access it in the browser, it says ‘transferring data from localhost’ and never ends – it’s like render doesn’t finish sending and never sends the headers. No errors, no logs, no nothing – res.render(‘index’) just hangs. The file exists, and the script finds it, but nothing ever happens. I don’t have a callback in the render defined, so headers should get sent as usual.
If on the other hand I replace the render command with a simple write(‘Hello world’); and then do a res.end();, it works like a charm.
What am I doing wrong with rendering? I haven’t changed a thing from the original installation btw. The file in question is index.ejs, it’s in views/, and I even called app.register(‘.ejs’, require(‘ejs’)); just in case before the render itself. EJS is installed.
Also worth noting – if I do a res.render(‘index’); and then res.write(‘Hello’); immediately afterwards, followed by res.end();, I do get “Hello” on the screen, but the render never happens – it just hangs and says “Transferring data from localhost”. So the application doesn’t really die or hang, it just never finishes the render.
Edit: Interesting turn of events: if I define a callback in the render, the response does end. There is no more “Transferring data…”, but the view is never rendered, neither is the layout. The source is completely empty upon inspection. There are no errors whatsoever, and no exceptions.
Problem fixed. It turns our render() has to be the absolute last command in a routing chain. Putting res.write(‘Hello’); and res.end(); after it was exactly what broke it.
I deleted everything and wrote simply res.render(‘index’) and it worked like a charm. Learn from my fail, newbies – no outputting anything after rendering!