Getting Started with Node.js and Express.js

What is Node.js?

At its core, Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows you to run JavaScript on the server side - outside of the browser. This means you can build backend applications, APIs, real-time chat apps, and much more, all using JavaScript.

What is Express.js?

Express.js is a lightweight, flexible Node.js web application framework that simplifies the process of building web applications and APIs. It provides a thin layer of fundamental web application features without hiding Node.js functionality.

Setting Up Your First Express.js App

Let's get your first app up and running.

Install Node.js

First, download and install Node.js from the official website: https://nodejs.org. This will also install npm, which you'll need for managing packages.

To check your installation, open a terminal and run:


node -v
npm -v

            

Create a New Project

Create a new directory and initialize a new project:


mkdir my-first-express-app
cd my-first-express-app
npm init -y

            

The -y flag automatically fills in default values in package.json.

Install Express

Next, install Express:


npm install express

            

Create the Server

Create a file called index.js and add the following code:


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

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

app.listen(port, () => {
    console.log(`Server is running at http://localhost:${port}`);
});

            

Run the Server

In your terminal, start the server:


node index.js

            

Visit http://localhost:3000 in your browser. You should see "Hello World from Express!".

🎉 Congrats — you just built your first Express.js server!