No easier Node.js method of HTML includes than this?

Including an HTML fragment with PHP couldn’t be easier. See here for an example: https://tryphp.w3schools.com/showphp.php?filename=demo_include1. (The footer.php file consists of nothing more than <footer>Copyright © 1999-2025 W3Schools.com</footer>.)

Now have a look at how includes seemingly have to be done with Node.js on https://stackoverflow.com/questions/16369649/include-html-blocks-using-node-js. Although the include lines themselves are easy as well, the required server.js file is complicated, to say the least.

Is there no easier Node.js method?

You need to appreciate that nodejs is server side and is not the same as javascript running in your browser. For instance it has no built in access to the browser’s DOM. So yes it is an involved process, that said the link you provided is outdated with posts from 12 years ago.

Here is a more up to date basic example.

I have typed in that tutorial here, so you can see the file structure and a working example. I have included a simple footer as an import, which is saved in a partials folder inside of views.

Here is the app.js code from that tutorial

const express = require("express");
const app = express();
const path = require("path");

// Set EJS as the template engine
app.set("view engine", "ejs");

// Set the views directory
app.set("views", path.join(__dirname, "views"));

// Routes

// homepage
app.get("/", (req, res) => {
  res.render("index", { userName: "John Doe" });
});

// products page
app.get("/products", (req, res) => {
  // sample product data
  const products = [
    { id: 1, name: "Laptop", price: 2000 },
    { id: 2, name: "Phone", price: 600 },
    { id: 3, name: "Tablet", price: 500 },
  ];

  // pass products data to the products template
  res.render("products", { products });
});

// Start the server
const PORT = 3000;
app.listen(PORT, () => {
  console.log('Server is running on localhost port 3000');
});

Note: It is better practice to have the routes in their own file which you would import, and even better practice to use a MVC pattern with controllers.

Here is a more detailed tutorial, which covers the use of layouts and partials

There are alternatives to ejs, for example pug which is a more minimal templating engine.

Long and short, if you plan to go down this path you will need to learn some nodejs. It is not going to be a 5 minute exercise.

Another link that might be useful.

3 Likes

Thank you very much! I’ll be studying those matters the coming days. But I have another question for now: can/should you have PHP and Node.js running at the same time?

With Node.js, you usually need extra setup like Express with a template engine (EJS, Handlebars) or something similar to handle HTML includes cleanly. Without those, it can feel clunky. So yeah, no real “one-liner” native method like PHP’s include - but once you set up a template engine, it’s not too bad and quite flexible.

1 Like

OK, I’ve gone over all the material. But I don’t see any easier method? Am I missing something? And if not, why would you want to use Node.js over PHP?

That’s a big question @fconijn, and one I am not really qualified to give you an answer on. That being said I don’t think you can come to a conclusive decision based on just one aspect e.g. templating your HTML.

It seems the main decisions on which to choose come down to project type, scalability/cost and familiarity.

I’m sure others on here can give you more indepth reasons, but here is just one article that might be informative.

Yes and no. On the one hand, there may indeed be a reason why to choose Node.js. On the other hand, server-side languages are mostly used for includes, aren’t they?

I had already read that article, but it doesn’t really answer my primary questions, also because some statements are outdated or simply not true (anymore). But I’ll ask the community here in another thread. Thanx again for you reply. :slight_smile: