This example creates a website using Node.js to provide logical website behavior. Using the Express.js framework, the website is implemented as a web application, with logical routing to other sections of the website.

The HTML and CSS is based on our responsive website design using CSS Grid and Flexbox. The HTML is refactored as a template, so layout code can be reused when adding new pages.

Install Node

Node.js, also called Node, is a runtime environment for writing server-side applications in JavaScript.

  • Install Node

  • Make a new Express app

  • Install nodemon

  • Add a development startup script

  • Preview the web app

  • HTML templates

  • Overview of Pug

  • Example HTML to Pug conversion

  • Overview of the default Express app

  • Implementation

  • App file structure

  • app.js

  • layout.pug

  • index.pug

  • index.js

  • style.css

  • menu.js

  • Secondary routes

  • Secondary views

  • Appearance

  • Install nodemon

  • Add a development startup script

  • Preview the web app

  • Overview of Pug

  • Example HTML to Pug conversion

  • App file structure

  • app.js

  • layout.pug

  • index.pug

  • index.js

  • style.css

  • menu.js

  • Secondary routes

  • Secondary views

Download the Node installer from the official Node.js downloads website. Choose the LTS (long term support) version for your operating system.

If Node is already installed on your computer, you can skip this section and proceed to make a new Express app.

Windows and macOS

Open and run the Node installer (.msi on Windows, .pkg on macOS).

On Windows, at the installation screen labeled Tools for Native Modules, check the box Automatically install the necessary tools.

Linux

On Linux systems, you can install Node using your package manager, install the compiled binaries manually, or build Node from source. For detailed information, refer to the official Node.js installation wiki.

All operating systems

When installation is complete, open a terminal or command prompt window. Run the following command to update npm, the Node package manager. The -g (global) switch specifies that the software is installed system-wide, not only the current Node app.

Windows

npm install -g npm

Linux and macOS

sudo npm install -g npm

Finally, use npm to globally install the express-generator application.

npm install -g express-generator

sudo npm install -g express-generator

Make a new Express app

In a terminal or command prompt window, generate a new Express.js app. In our example, the app name is myapp, and the view engine is specified as pug.

express myapp –view=“pug”

Change directory to the new Express app.

cd myapp

In the Express app directory, use npm install to download and install the required dependencies, as listed in the package.json file.

npm install

If any security updates are available for the installed dependencies, a notification is displayed.

found 1 low severity vulnerability run npm audit fix to fix them, or npm audit for details

If so, apply the security updates.

npm audit fix

Install nodemon

In the Express app directory, install nodemon. The option –save-dev indicates that nodemon is a development dependency. It is not used in the application itself, but is a tool used during development.

npm install –save-dev nodemon

Add a development startup script

A development startup script provides a way to start your web application with options that help you develop the app, such as verbose error messages.

In a text editor, open the file package.json in the app directory. This JSON file specifies the dependencies used by your Node app. Additionally, it contains named startup scripts that start the application in different ways.

In package.json, locate the “scripts” entry. By default, it contains only one script (“start”).

“scripts”: { “start”: “node ./bin/www” },

Add a new line that defines a script devstart as follows.

“scripts”: { “start”: “node ./bin/www”, “devstart”: “DEBUG=myapp:* nodemon ./bin/www” },

“scripts”: { “start”: “node ./bin/www”, “devstart”: “SET DEBUG=myapp:* & nodemon ./bin/www” },

These scripts (“start” and “devstart”) can be executed by running the command npm run scriptname.

The command npm run devstart starts the app with two additional development features enabled.

  • The DEBUG environment variable is set, specifying that the console log and error pages, such as HTTP 404, display additional information, like a stack trace.
  • In addition, nodemon monitors certain important website files. If you modify these files, such as redesigning a page or modifying static content, nodemon automatically restarts the server to reflect the changes.

Start the web server in development mode.

npm run devstart

Preview the web app

When the application is running, your computer acts as a web server, serving HTTP on port 3000.

If the Windows Firewall blocks the web server application, click Allow Access.

To preview the website, open a web browser to the address localhost:3000.

Any device connected to your local network can view the application at address ipaddress:3000, where ipaddress is the local IP address of the computer running the app.

To preview the website on a mobile device, connect its Wi-Fi to your local network, and open the address in a browser.

If you’re not sure what the computer’s local IP address is, see: How to find my IP address.

HTML templates

Our example uses the CSS, JavaScript, and HTML from the how to create a responsive website using CSS Grid and Flexbox. The CSS and JavaScript are used verbatim. The HTML is refactored to a templating language.

Using a templating language, the layout code is written only once, and inherited by other pages.

The software that converts a template to its final format is called a template processor. In the context of HTML, a template processor is called a view engine.

Express.js supports several view engines, including Pug.

Overview of Pug

The Pug language describes HTML documents, in a way that provides benefits and additional features. Pug files are rendered to HTML when the user requests them.

Pug’s language syntax removes the need for tags to be closed, or enclosed in brackets. It also supports inherited templates, iteration, conditionals, and JavaScript evaluation.

Example HTML to Pug conversion

These are the first few lines of the HTML from the how to create a responsive website using CSS Grid and Flexbox.

Title