Building “Hello World” with Express.js!!

Posted by

Hello World Using Express js

Hello World Using Express js

Express js is a popular Node.js web application framework that provides a robust set of features for building web applications. In this article, we will create a simple “Hello World” application using Express js.

Setting up the environment

Before we dive into coding, we need to make sure that Node.js is installed on our system. If not, we can download it from the official website and follow the installation instructions. Once Node.js is installed, we can use npm to install Express js by running the following command in the terminal:

npm install express

Creating the Hello World application

Now that we have Express js installed, we can create a new file called “app.js” and write the following code:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World');
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

In this code, we imported the Express js module and created a new Express application. We then defined a route for the root URL (“/”) and specified that the server should respond with “Hello World” when this route is accessed. Finally, we told the server to listen on port 3000 and log a message to the console when it starts.

Running the application

To run the application, we can navigate to the directory where “app.js” is located and run the following command in the terminal:

node app.js

Once the server is running, we can open a web browser and navigate to “http://localhost:3000” to see the “Hello World” message displayed on the screen.

Congratulations!

With just a few lines of code, we have created a simple “Hello World” application using Express js. This demonstrates the power and simplicity of the Express js framework for building web applications.

Happy coding!