Deploying A Localhost Server With Node.js and Express.js

Deploying A Localhost Server With Node.js and Express.js

Starting With Node.js & Express.js

Objective

The objective is simple. Deploy a localhost server using Node.js and Express.js.


But first, you may be wondering what are Node.js and Express.js?

Node.js (Node) is an open-source server-side platform built on Google Chrome’s V8 JavaScript engine. Due to its ability to run on various platforms and its scalability, Node has become a “go-to” option for developing web applications.

Express.js (Express) is a popular module framework that runs on Node. It empowers developers to quickly build static and dynamic web applications and APIs with ease.

In more simplistic terms, Node allows you to run JavaScript outside of your browser, and Express enables you to respond to individual client requests and build APIs quickly.

Enough talk. Let’s get started!

Step 1: Download Node

Download Node.js and follow the guided prompts until the installation is complete.

You can validate Node was successfully installed by navigating to the command line and entering the following command.

node --version        

If installed correctly, you will see the version of Node returned. For example, I am currently running on V18.0.

Step 2: Create A New Working Directory in htdocs

Before installing Express, you will want to create a new working directory.


mkdir myfirstnodeproject        

Next, add the two files below.

  • index.html
  • app.js


touch index.html app.js        

Step 3: Install Express

Once the two files are added to your project, you are set to install Express. First, make sure you are in the working directory you just created. For my example, I would be in “Example-1”. You can check to see what directory you are currently in by entering “pwd” on your command line. Once confirming you are in the correct directory, install Express by running the command below.


npm install express        

Step 4: Set Up Your HTML

Open the working directory you created in your code editor. Currently, I’m running on VS (Visual Studio), but that is just my preference. You can use whatever works best for you! For this example, insert a <h1> header in the body to show how the browser interacts with your server.


Type the code below into your “index.html” file.

<!DOCTYPE html>
<html lang="en" dir="ltr">
 <head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Example-1</title>
 </head>
 <body>
  <h1>You did it!</h1>
 </body>
</html>        

You may have noticed that your project now holds two new files. These files were added to your project when you installed Express and provide access to the pre-built modules and packages. Remember when I mentioned Express is a framework that allows developers to build web applications easily and quickly?

  • node_modules: Pre-built packages that can be used in your project
  • package-lock.json: Stored dependency tree

Step 5: Set Up Your Localhost Server

You are now ready to launch your localhost server!


Enter the following code into your “app.js” file.

const express = require("express");

const app = express();
app.get("/", function (req, res) {
    res.sendFile(__dirname + "/index.html");
});
app.listen(3000, function () {
    console.log("Server is running on localhost3000");
});        

Step 6: Launch Your Server

You are almost there! Go to the command line and enter the following in order to launch your server:


node app.js        

You will know your server is running correctly if your terminal returns the “Server is running on localhost3000” message written the “app.js” file.

console.log("Server is running on localhost3000");        

Step 7: Check it out!

Finally, go to Chrome and enter “localhost:3000” in your browser. You should now see the <h1> header you wrote in the “index.html” file.

If you received an error message, please go back and review steps 1–7 for discrepancies. It’s easy to miss a bracket or semicolon!

Recap

Congratulations! You now have a solid understanding of how to build and test your web applications on a localhost server using Node and Express.

To view or add a comment, sign in

More articles by Syed Bilal Ali

  • Building My First Shopify Custom App Using Remix

    As a developer, we often encounter projects that challenge us, push our boundaries, and ultimately help us grow…

    4 Comments
  • A Comprehensive Guide on Integrating Firebase with Nest JS

    Nest JS has become a popular backend framework. However, there is a lack of good articles or guides on how to integrate…

    4 Comments
  • Vite VS Webpack Mix

    Laravel Mix and Laravel Vite are the two options available to developers for front-end development in the framework…

  • Setting Up a LAMP Server on VPS Ubuntu (Apache2)

    In the realm of web development, mastering the setup of a LAMP (Linux, Apache, MySQL, PHP) server on Ubuntu is a…

  • Payment Process with Terminal Transaction

    Recently, I had the opportunity to dive deep into the world of Wallee Payment Gateway integration, and let me tell you,…

    3 Comments
  • Remove Image Background using NodeJS

    Introduction: In today's digital world, images play a crucial role in various applications, from e-commerce websites to…

    2 Comments
  • NestJS unit testing

    This tutorial is a deep dive into unit testing in NestJS (including mocking with test doubles). To get the most out of…

    1 Comment
  • Pseudo-Palindromic Paths in a Binary Tree

    Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be…

  • JQuery ContextMenu plugin

    Designed for web applications that require menus on a potentially huge number of items, the contextMenu Plugin In…

  • Building a Scalable Microservice Architecture for Nest.js Projects

    1. Introduction to Microservices 2.

Insights from the community

Others also viewed

Explore topics