DEV Community

FredAbod
FredAbod

Posted on

Introduction to ExpressJs

Introduction to ExpressJS

ExpressJS is a fast, minimal, and flexible web application framework for Node.js. It provides a robust set of features for building web applications and APIs. ExpressJS is widely used due to its simplicity, scalability, and extensibility. It is built on top of Node.js's HTTP module, making it easy to handle HTTP requests and responses.

Why ExpressJS?

ExpressJS is popular among developers for several reasons:

  • Simplicity: ExpressJS is designed to be minimal and straightforward, providing just enough tools to build web applications without unnecessary complexity.

  • Flexibility: ExpressJS allows developers to structure their applications in a way that suits their needs. It does not enforce any specific architecture or folder structure, giving developers the freedom to organize their code as desired.

  • Middleware: ExpressJS has a powerful middleware system that enables developers to add functionality to their applications at various stages of the request-response cycle. Middleware functions can be used for tasks like authentication, logging, error handling, and more.

  • Routing: ExpressJS provides a simple and intuitive routing mechanism, making it easy to define endpoints and handle different HTTP methods (GET, POST, PUT, DELETE, etc.).

  • Large Ecosystem: ExpressJS has a vibrant and active community, resulting in a vast ecosystem of third-party libraries and plugins that extend its functionality. This makes it easier to add features to your application without reinventing the wheel.

Setting up Express and Basic Routing

To get started with ExpressJS, follow these steps:

  1. Install Node.js: Ensure that you have Node.js installed on your system. You can download it from the official Node.js website (https://nodejs.org) and follow the installation instructions.

  2. Create a New Project: Create a new directory for your Express project and navigate to it using the command line.

  3. Initialize the Project: Run the following command to initialize a new Node.js project:

npm init -y
Enter fullscreen mode Exit fullscreen mode

This will provide information about your project and create a package.json file.

  1. Install Express: Install Express as a dependency for your project using the following command:
npm i express
Enter fullscreen mode Exit fullscreen mode

This will download and install Express in your project's node_modules folder.

  1. Create an Entry Point: Create a new file, for example, app.js or index.js, as the entry point of your application.

  2. Require Express and Create an Instance: In your entry point file, require the Express module and create an instance of the Express application:

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

7. **Basic Routing**: Set up basic routes for handling HTTP requests. For example:
app.get('/', (req, res) => {
  res.send('Hello, Express!');
});

app.post('/users', (req, res) => {
  // Handle POST request to create a new user
});

app.put('/users/:id', (req, res) => {
  // Handle PUT request to update a user by ID
});

app.delete('/users/:id', (req, res) => {
  // Handle DELETE request to delete a user by ID
});
Enter fullscreen mode Exit fullscreen mode

In the above code, we define routes for the root path (/), a POST request to /users, a PUT request to /users/:id, and a DELETE request to /users/:id. Each route specifies a callback function to handle the request and send an appropriate response.

Now that you know the basics and why we use express lets create a simple express app that'll say HELLO WORLD
In your root file(index.js or app.js)

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

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

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Save the file
Start your Server by running node index.js or node app.js
You should see the message "Server is running on port 3000" in the console, indicating that the server has started.
Open your web browser and visit http://localhost:3000. You should see the message "Hello, World!" displayed in the browser.

Congratulations! You've created a simple Express app that responds with "Hello, World!" when accessed through the root path😎😎😎

Top comments (0)