๐ŸŒ Node.js Express & API Development
Estimated reading: 3 minutes 617 views

Node.js โ€“ Express Framework โ€“ Build Web Apps and REST APIs Easily


Introduction โ€“ What Is Express.js?

Express.js is the most popular and lightweight web application framework for Node.js. It simplifies the process of building robust web servers and APIs by providing a minimal but powerful set of features like routing, middleware, request handling, and static file servingโ€”all built on top of Node’s native http module.

In this guide, youโ€™ll learn:

  • How to set up and use Express
  • Create routes and handle different HTTP methods
  • Serve HTML, JSON, and static files
  • Build REST APIs with ease

Step 1 โ€“ Install Express

npm install express

Step 2 โ€“ Create a Basic Express App

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

// Define route
app.get('/', (req, res) => {
  res.send('Welcome to Express.js!');
});

// Start server
app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

Output (Browser):

Welcome to Express.js!

Express Routing โ€“ Handle Multiple Routes

app.get('/about', (req, res) => {
  res.send('About Page');
});

app.post('/contact', (req, res) => {
  res.send('Contact Form Submitted');
});
MethodRouteDescription
GET/aboutShows About content
POST/contactHandles form submission

Serve HTML Files Using res.sendFile()

const path = require('path');

app.get('/home', (req, res) => {
  res.sendFile(path.join(__dirname, 'index.html'));
});

index.html will be served from the server root directory.


Serve Static Files (CSS, JS, Images)

app.use(express.static('public'));

Place all static files inside the /public directory.


Sending JSON Responses

app.get('/api/data', (req, res) => {
  res.json({ message: 'Hello JSON!', status: 'OK' });
});

Output:

{
  "message": "Hello JSON!",
  "status": "OK"
}

Route Parameters and Query Strings

app.get('/user/:id', (req, res) => {
  res.send(`User ID: ${req.params.id}`);
});

app.get('/search', (req, res) => {
  res.send(`Query: ${req.query.q}`);
});

/user/123 โ†’ User ID: 123
/search?q=nodejs โ†’ Query: nodejs


Middleware in Express

Middleware are functions executed during the request lifecycle. Example:

app.use((req, res, next) => {
  console.log(`${req.method} request to ${req.url}`);
  next();
});

Use middleware for:

  • Logging
  • Authentication
  • Parsing request bodies
  • Error handling

Summary โ€“ Recap & Next Steps

The Express framework provides a powerful abstraction over Node.js’s http module, making it the go-to choice for building scalable web apps and APIs. It’s flexible, fast, and supports middleware, routing, templating, and much more.

Key Takeaways:

  • Express simplifies HTTP server creation
  • Create clean route handlers for GET, POST, etc.
  • Easily send HTML, JSON, and serve static files
  • Use middleware to enhance request handling

Real-world relevance:
Express is used in RESTful APIs, eCommerce sites, real-time dashboards, authentication systems, and microservices.


FAQs โ€“ Node.js Express Framework


Is Express part of Node.js by default?
No. You must install it manually using npm install express.


Whatโ€™s the difference between res.send() and res.json()?
res.send() sends any content type. res.json() formats the output as JSON.


Can I handle file uploads in Express?
Yes. Use middleware like multer to handle multipart form data.


How do I handle errors in Express?
Use error-handling middleware:

app.use((err, req, res, next) => {
  console.error(err);
  res.status(500).send('Something broke!');
});

Is Express.js suitable for production apps?
Absolutely. It powers production apps at scale, often paired with tools like Nginx, Docker, or cloud platforms.


Share Now :
Share

๐Ÿš€ Node.js โ€“ Express Framework

Or Copy Link

CONTENTS
Scroll to Top