Love ReactJS? Check out how Next.js uses React and Express for easy server-side rendering.
Node.js makes it remarkably straightforward for developers to launch a local server. This tutorial demonstrates using the Express framework to create a simple web application running on a local Node.js server.
Key Takeaways
- Learn how to set up a Node.js environment and project structure.
- Understand the basics of Express.js for server-side applications.
- Get hands-on with creating a basic web server with minimal setup.
- See how easy it is to run and test your server locally.
Steps
This tutorial assumes you're starting from scratch. It includes:
- 1. Installing Node.js
- 2. Creating a Node.js project
- 3. Adding the Express module
- 4. Launching the app
Installing Node.js
The first step is to install Node.js on your machine. Visit the official Node.js website and download the latest stable version for your platform.
Follow the installation instructions specific to your OS. After installation, verify with:
node --version
If Node.js is installed successfully, you should see a version number, such as v20.x.x.
Creating a Node.js project
Using your command line, navigate to your preferred projects directory and create a new folder named 'myproject':
mkdir myproject
cd myproject
Initialize a new Node.js project, which will generate a package.json file by running:
npm init -y
This command creates a package.json with default settings, allowing you to edit it as needed later. Ensure the "main" attribute points to index.js, which will be the entry point for your app.
Create the main script file:
touch index.js
Adding the Express.js module
To start building your web server, you need to install Express. Express simplifies routing and server management. Install Express and set up your server with:
npm install express
Edit index.js in your favorite text editor and add:
// Import Express module
const express = require('express');
// Define port for local web server
const port = 5000;
// Initialize your Express app
const app = express();
// Create a simple GET route for the index of your app
app.get('/', (req, res) => {
res.send('Hello World');
});
// Listen on the defined port
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
This script sets up a basic web server with a single route.
Launching the app
To launch your server, navigate to the project's root directory and run:
node index.js
If everything is set up correctly, you'll see:
Listening on port 5000
Visit http://localhost:5000 in your browser, and you should see ‘Hello World!’
Congratulations! You've set up a Node.js web server with Express.
FAQ
What is Node.js best used for?
Node.js excels in building fast, scalable network applications, especially web servers and API services, due to its non-blocking, event-driven architecture.
Is Express still relevant in 2026?
Yes, Express remains a popular framework for creating RESTful APIs and web applications due to its simplicity, robust middleware ecosystem, and flexibility.
Can I use something other than Express for a Node.js server?
Yes, there are many alternatives to Express like Koa, Fastify, and NestJS, each offering different features and performance benefits.
