Has anyone created a Node.js app that renders HTML instead of JSON?

I'm working on a side project that really benefits from an asynchronous web framework, so I decided to try Node.js. From the tutorials I've seen online, it seems that most Node developers are using it for a JSON API that connects to an SPA frontend. I would prefer to do server rendered templates instead. If you've made a site like this before, what templating engine did you use and what did you use for integration tests? I appreciate any tips!

Did you find this post useful? Show some love!

I started making express apps that rendered pug templates.

Loved the lean syntax.

Sorry I don't have a recommendation on a client side testing framework. I imagine most of the major frameworks use some node library that can probably be ripped out and applied to just inspecting your rendered pages.

I just started using pug templating too. It looks clean (and a bit odd at forst) but server side logic is amazing. You don't even need a framework at that point (for small stuff).

I second this.

I was recently introduced to pug and it's really simple to use : )

It's been a while but I think we used Jade (default for express I think). Looking at EJS.

It seems likely these templating languages are inspired by Rails options. EJS is like ERB and Jade looks a lot like HAML.

My experience with Rails is that it's a pretty good idea to go with stuff that looks more like HTML in the end. Jade (and HAML) are usually cleaner to look at but ultimately it's easier to go with what people will pick up quickly because it's closer to what they know.

That’s good advice, I normally avoid the HAML like templates mostly because I don’t want to take the time to learn another HTML. I’ve been trying out Handlebars, but I’ll take a look at EJS and see how it goes. Do you guys use any Node on the server for Dev.to?

Do you guys use any Node on the server for Dev.to

Nope. We have some Node experience on the team but no Node use. Rails backend with some AWS Lambda in the mix.

Handlebars allows you to have some template sharing (main app template and partials) whereas EJS doesn't.

You can also take a look at Nunjucks. It's a Jinja2 like template engine.
It has support for features like: variables, template inheritance, macros, filters and it plays well with async programming.


Hey there, we see you aren't signed in. (Yes you, the reader. This is a fake comment.)

Please consider creating an account on dev.to. It literally takes a few seconds and we'd appreciate the support so much. ❤️

Plus, no fake comments when you're signed in. 🙃

I second pug/jade.

Note that for simple use cases, one can do things like:

app.get('/', (req, res) => res.set('content-type', 'text/html').end(`
  <h1>It works!</h1>
  <p>You can send your HTML string in via ES6 template strings!</p>
  <p>Node version: ${process.versions.node}</p>

For me it's whatever is the easiest for my brain to understand whilst not losing interest or momentum in the project, but I am really wondering why you'd want to render html instead of JSON or send server-rendered templates instead.

I, lazy. Get stuff out on site generator. I wouldnt get to using a JS framework unless the situation calls for it (too many pages, lots of data handling).

I laughed at reading this post. We're so futuristic and so meta that nobody renders HTML on the server anymore lest they be reprimanded by those who know better ™ :D

For templating in Node, I've used Handlebars.

I think at the end it will always come back with Pug, formerly Jade. It's really easy, but if you would like something more like Html, then EJS I think.

Classic DEV Post from Jun 7

10 Things I Regret About Node.js

Photo courtesy of flickr user Trygve Lie. The creator of Node JS, Ryan Dahl, r...

Follow @nickytonline to see more of their posts in your feed.
Andrew Davis
PHP and JavaScript web developer with a little Swift and Go mixed in.
Trending on dev.to
Does your website really need to be larger than Windows 95?
#discuss #coding #technology
If you don't hire juniors, you don't deserve seniors
#career #management #recruiting
Thoughts on interpreted vs compiled languages?
What is difference among enterprise architect, solution architect, software architect, system architect and cloud architect?
Lessons I learned from training with special forces that I use in software development
#beginners #career #motivation #dieliving
Have you ever been in a hackathon
When signing up or logging into an app, do you prefer OAuth (authentication w/social media account) or email sign up/sign in?
Why Your Best Work is Hardest to Finish
#productivity #career #beginners