How To Create A Basic Express Web App

How To Create A Basic Express Web App

How do i create my first express web app ?

ยท

2 min read

What is Express js?

Express.js is a popular web application framework for Node.js. It provides features for building web applications and APIs, such as middleware for handling requests, routing for defining URL endpoints, and templating engines for rendering HTML pages.

Express.js allows developers to easily create robust and scalable web applications by providing a simple and intuitive API for handling HTTP requests and responses. It also supports a variety of plugins and extensions that can be used to add additional functionality to an application.

Overall, Express.js is a popular and widely-used framework for building web applications and APIs using Node.js, due to its simplicity, flexibility, and performance.

To create a basic Express web app, you'll need to follow these steps:

step 1: Install Node.js and NPM

Express is a Node.js module, so you'll need to have Node.js and NPM installed on your machine. You can download and install them from the official Node.js website.

npm install -g npm

Step 2: Create a new project directory

Create a new directory for your project using the terminal or command prompt.

$ mkdir my-express-app

Change into the newly created folder:

$ cd my-express-app

Initialize a new Node.js project

In the terminal, navigate to your project directory and run npm init to create a new Node.js project. This will create a package.json file, which will store information about your project.

$ npm init -y

Step 3: Install Express

Run npm install express to install the Express module.

$ npm install express

Step 4: Create your app file

Create a new file in your project directory called app.js and add the following code:

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

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

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

This code sets up a new Express app and listens on port 3000. When a user navigates to the root URL (/), the app sends a response with the text "Hello World!".

Step 5: Start your app

Run node app.js to start your app. You should see a message in the console saying "Server started on port 3000".

$ node app.js

step 6: Test your app

Open your web browser and navigate to http://localhost:3000. You should see the text "Hello World!" displayed on the page.

Congratulations, you have created a basic Express web app! ๐Ÿ™‚

ย