Uncategorized

How to create a website using Node.js and Express

Node.js Web Application

This example creates a website using Node.js to provide a 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.

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

Reading: How to create a website with node js

Install Node

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

Download the Node installer from the official Node.js download site. Select the LTS (Long Term Support) version for your operating system.

Windows and macOS

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

On Windows, on the installation screen labeled Tools for Native Modules, check the Install required automatically check box 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, see the official Node.js installation wiki.

All operating systems

Once the 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 will be installed system-wide, not just the current node app.

Windows

npm install -g npm

Linux and macOS

sudo npm install -g npm

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

Windows

npm install -g express-generator

Linux and macOS

sudo npm install -g express-generator

Create a new express app

Generate in a terminal or command prompt window 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 shown in the package.json ”.

p> npm install

If security updates are available for the installed dependencies, a notification will be displayed.

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

If yes, apply the security fixes.

npm audit fix

Install nodemon

Install in the Express App Directory nodemon. The -save-dev option specifies 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

Adding a startup script for development

A Development startup script provides a way to start your web application with options to help you develop the app, e.g. For example, detailed error messages.

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

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

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

Add a new line defining a script devstart as follows.

Linux and macOS

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

Windows

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

These scripts (“start” and “devstart”) can be run by running the npm run Script name.

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

  • The Die Environment variable DEBUG is set and specifies that the console log and error pages such as HTTP 404 display additional information such as a stack trace.
  • Additionally nodemon monitors certain important website files. If you change these files, e.g. For example, if you redesign a page or change static content, nodemon will automatically restart the server to reflect the changes.

Start the web server in development mode.

npm run devstart

Preview the web application

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

See also: How to Build a Subscription Website (Step by Step Tutorial)

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

Default Express.js app

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

To preview the site on a mobile device, connect its WiFi to your local network and open the address in a browser.

Express.js on mobile

HTML templates

Our example uses CSS , JavaScript and HTML from Building a Responsive Website with CSS Grid and Flexbox. CSS and JavaScript are used verbatim. The HTML code is transformed into a template language.

When using a template language, the layout code is only written once and is adopted 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.

Pug overview

The Pug language describes HTML documents in a way that offers benefits and additional functionality. Pug files are rendered in HTML when the user requests them.

Pug’s language syntax eliminates the need to close tags or enclose them in parentheses. It also supports inherited templates, iterations, conditionals, and JavaScript evaluation.

Example HTML to Pug conversion

These are the first few lines of HTML from the Creation of a responsive website with CSS Grid and Flexbox.

Title

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button