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.
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.
Let's get your first app up and running.
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 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.
Next, install Express:
npm install express
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}`);
});
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!