Htmx & express: beginner question


I have a quick question regarding code organization. Currently, I’m using my main page index.html file, which contains only HTML code for the navigation bar. I’m using htmx to dynamically render content based on the selected navigation element on the server side (using express) . As a result, most of my HTML structure, apart from the navigation bar itself, resides in my server.js file.

However, I’m unsure if this approach is the most effective. My index.html file appears quite empty, while my server.js file feels overloaded with code, as it handles not only rendering HTML but also includes additional functionality. As someone new to web development, I’m curious about the typical structure for organizing code in a small web application and how different files can interact with each other.

Any insights or guidance on best practices would be greatly appreciated

1 Like


Instead of embedding HTML directly in your server.js, consider using template engines like Handlebars, EJS, or Pug. This allows you to keep HTML structure in separate files (like templates or views), making them easier to manage and update.

You can also break down your server.js into smaller modules. For instance, routing can be in separate files, and middleware functions can also be modularized.

I wrote a couple of tutorials on creating an app using Express. Maybe these can help.

And more recently:

Happy to discuss if you have any concrete questions.


Or you consider to totally separate frontend and backend and use nodeJS only for backend, while you use another Webserver for the frontend serving your html, css and vanilla js scripts.


Thank you for the responses, and I apologize for the delay in replying. Currently, I have implemented several HTML files. The main file, index.html, contains navigation links, and clicking on them loads corresponding content from other html files into the main div.

Therefore, I am not using any template engine yet. However, working on the login function it gets more messy. Would you recommend sticking with the current approach?

1 Like

Hi, it would be easier to offer an opinion if we could see the code. Would it be possible for you to stick your project up on GitHub? That way, I could clone it and run it on my PC.