- Authors
- Name
- Nguyễn Đức Xinh
- Published on
- Published on
Create a First ExpressJS Application
Express is a popular web framework for Node.js, designed to help you quickly and easily build web applications and APIs. This article will guide you in creating a simple Express application and configuring basic routes.
Prerequisites
Before starting, ensure you have:
- Node.js and NPM installed on your machine (if not, refer to the guide linked below).
- Basic knowledge of JavaScript and Node.js .
- A text editor (e.g., VS Code ).
Step 1: Install Node.js and Express
1. Install Node.js
If you haven’t installed Node.js yet, download and install it from nodejs.org . Verify the installation by running:
node -v
npm -v
If you need help, check this guide: Installing NodeJS and NPM on macOS, Windows, and Linux .
2. Initialize a Node.js Project
Open your terminal and create a new directory for your Express application:
mkdir express-example-app
cd express-example-app
3. Install Express
Install Express in your project. We’ll use express-generator
for faster setup:
# Install express-generator globally
npm install -g express-generator@4
# Use express-generator to create a quick setup
npx express-generator --git
4. Install Dependencies
Run the following command to install project dependencies:
npm install
5. Start the Application
After setting up your application, start it with:
npm start
Your application will run on port 3000 (or the port specified in the PORT
environment variable). You can access it in your browser at http://localhost:3000 .
Directory Structure
The default directory structure generated by express-generator
version 4 looks like this:
Step 2: Create the Main Application File
Create a new file app.js
in your project directory:
touch app.js
Open app.js
and configure a basic Express application:
const express = require('express');
const app = express();
// Configure the port for the application
const PORT = process.env.PORT || 3000;
// Basic route
app.get('/', (req, res) => {
res.send('Hello World!');
});
// Start the application
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Auto-Reload Configuration
Install nodemon
to automatically restart the Node.js server when files change:
npm install nodemon -g
Update the scripts
section in package.json
:
"scripts": {
"start": "nodemon ./bin/www",
"start:watch": "nodemon ./bin/www"
},
Run the application with:
npm run start:watch
Step 3: Create a Simple API
Add a New Route
Create a new file routes/products.js
with the following content:
const express = require('express');
const router = express.Router();
/* GET products listing. */
router.get('/', (req, res) => {
const products = [
{ id: 1, name: 'Laptop', price: 1000 },
{ id: 2, name: 'Mobile', price: 500 },
{ id: 3, name: 'Tablet', price: 300 },
];
res.json(products);
});
module.exports = router;
Register the Route
Open app.js
and add the following:
const productsRouter = require('./routes/products');
app.use('/api/products', productsRouter);
Test the API
Visit http://localhost:3000/api/products in your browser or Postman to see the results.
Conclusion
Congratulations! You’ve successfully created a simple Express application with basic routes. Express is a powerful and flexible framework that allows you to quickly build web applications and APIs.
Source Code
View the demo source code here: https://github.com/ducxinh/express-example-app
Next Steps
Here are some ways to expand your project:
- Routing, Middleware, and Error Handling
- Deployment : Deploy the app to a server or cloud platforms like Heroku, AWS, or DigitalOcean.
- Database Integration : Connect to databases like MongoDB, MySQL, or PostgreSQL for data storage and retrieval.
- Authentication : Secure routes using JWT or OAuth for user authentication.
- API Routes : Build RESTful API routes for frontend applications.