Express.js Hello World: Building Your First Web Application

Express.js is a powerful and minimalistic web application framework for Node.js that simplifies the process of building web applications. It’s a great choice for both beginners and experienced developers due to its ease of use and flexibility. In this article, we’ll guide you through creating your first “Hello World” web application using Express.js.

Prerequisites

Before you begin, make sure you have Node.js installed on your system. You can download and install Node.js from nodejs.org. After you have Node.js up and running, you can start building your first Express.js application.

Setting Up Your Project

  1. Create a New Directory: Start by creating a new directory for your project. You can do this through your terminal or command prompt with the following command:
   mkdir my-express-app
  1. Navigate to Your Project Directory: Change your current working directory to the newly created one:
   cd my-express-app
  1. Initialize Your Project: Initialize a Node.js project by running the following command:
   npm init -y

This will create a package.json file with default values.

  1. Install Express.js: To use Express.js, you need to install it as a dependency for your project. Run the following command to install Express.js:
   npm install express

Creating Your Express Application

With your project set up, it’s time to create your Express.js application. For this “Hello World” example, we’ll create a simple web server that responds with “Hello, World!” to any incoming request.

  1. Create an app.js file: In your project directory, create a file named app.js. This is where we’ll write our Express.js code.
  2. Open app.js in your preferred code editor: You can use any code editor of your choice, such as Visual Studio Code, Sublime Text, or Atom.
  3. Write the Express.js Code:
   // Import the Express.js framework
   const express = require('express');

   // Create an instance of Express
   const app = express();

   // Define a route that responds with "Hello, World!"
   app.get('/', (req, res) => {
     res.send('Hello, World!');
   });

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

In this code:

  • We import the Express.js framework.
  • Create an instance of Express.
  • Define a route that responds with “Hello, World!” when someone accesses the root URL (/).
  • Start the Express server on port 3000, and a message is displayed in the console to confirm that the server is running.
  1. Start Your Application: To start your Express application, run the following command in your terminal:
   node app.js

You should see the message “Server is running on http://localhost:3000” in the console.

  1. Test Your Application: Open a web browser and navigate to http://localhost:3000. You should see the text “Hello, World!” displayed in your browser, confirming that your Express.js application is up and running.

Congratulations! You’ve successfully created your first Express.js “Hello World” web application. You can now expand upon this foundation to build more complex and feature-rich web applications using Express.js. Whether you’re creating a simple API, a web service, or a full-fledged web application, Express.js offers the tools and flexibility to make development straightforward and efficient.


Posted

in

,

by

Tags:

Comments

Leave a Reply

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