Express.js Basics with Code Samples

Introduction

If you’ve ever thought of building a backend with Node.js but felt overwhelmed by the setup or the complexity, Express.js is your answer. It’s one of the most popular backend frameworks used by developers to build efficient and scalable web applications with minimal effort.

Express.js Basics with Code Samples

In this blog, we’ll walk you through the fundamentals of Express.js, its real-world usage, and simple code samples to get started all in plain English. Whether you're a beginner or brushing up your skills, this guide is for you. 

Also, if you're serious about mastering backend development with Node.js and Express.js, check out Uncodemy’s Full Stack Development with Node.js Course for a deeper dive. 

What is Express.js? 

Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for building single-page, multi-page, or hybrid web applications. 

In simpler terms, Express makes it easy to create a web server and handle HTTP requests and responses without writing long lines of repetitive code. 

Why Use Express? 

  • Simplifies the process of routing, middleware management, and server handling 
  • Lightweight and unopinionated (you can structure your app as you like) 
  • Supported by a strong community and plenty of middleware 
  • Easily integrates with databases like MongoDB, MySQL, and others 

Installing Express 

To start with Express, you’ll need Node.js installed on your machine. You can check with: 

Copy Code

node -v 

npm -v

Step 1: Create a Project Folder 

Copy Code

mkdir my-express-app 

cd my-express-app 

npm init -y

 

Step 2: Install Express 

npm install express 

Now you're ready to roll. 

Your First Express Server 

Create a file named app.js and paste the following code: 

Copy Code

const express = require('express'); 

const app = express(); 

const port = 3000; 

app.get('/', (req, res) => { 

  res.send('Welcome to Express.js!'); 

}); 

app.listen(port, () => { 

  console.log(`Server running on http://localhost:${port}`); 

});

How it Works: 

  • express() initializes your app 
  • app.get() handles GET requests 
  • res.send() sends a response back to the client 
  • app.listen() starts the server on the defined port 

Middleware in Express 

Middleware functions are functions that have access to the request and response objects. They can execute any code, modify the request/response, or end the response cycle. 

Here’s a simple example: 

Copy Code

app.use((req, res, next) => { 

  console.log('Request received at:', new Date()); 

  next(); 

});

This middleware logs the time when a request is made. 

Routing in Express 

Routing in Express is about defining how your app responds to different HTTP methods and URL paths. 

Copy Code

app.get('/about', (req, res) => { 

  res.send('About Us'); 

}); 

app.post('/contact', (req, res) => { 

  res.send('Contact form submitted'); 

});

You can use GET, POST, PUT, DELETE, etc., depending on what your app needs to handle. 

Handling Form Data 

To handle data sent by forms (POST requests), you can use middleware like express.urlencoded and express.json: 

Copy Code

app.use(express.urlencoded({ extended: true })); 

app.use(express.json()); 

app.post('/submit', (req, res) => { 

  console.log(req.body); 

  res.send('Form data received'); 

});

This makes it easy to extract and use data submitted via forms or APIs. 

Using Static Files 

Want to serve images, CSS, or JS files? Just use this: 

Copy Code

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

Now any file inside the public folder can be accessed directly via the browser. 

Error Handling 

Express allows you to define error-handling middleware as well: 

Copy Code

app.use((err, req, res, next) => { 

  console.error(err.stack); 

  res.status(500).send('Something went wrong!'); 

});

This ensures users get proper feedback and you can handle crashes gracefully. 

Conclusion 

Express.js is beginner-friendly yet powerful enough for professional-grade web apps. With just a few lines of code, you can create routes, handle data, and build scalable applications without the complexity. 

Learning Express.js gives you a strong foundation in backend development and opens doors to full-stack development. 

Ready to Dive Deeper? 

If this sparked your interest and you want to master backend development with real projects, don’t miss the Full Stack Node.js Course at Uncodemy. It’s designed for learners like you, with expert guidance, real-world projects, and career support. 

FAQ: Express.js Basics 

Q1: Is Express.js only for APIs? 
No. Express can be used to build web apps, REST APIs, single-page apps, and even handle server-side rendering. 

Q2: Can I use a database with Express? 
Yes. Express can easily connect with MongoDB, MySQL, PostgreSQL, and other databases using ORMs or native libraries. 

Q3: Is Express good for large-scale applications? 
Absolutely. It’s widely used in production by companies like Uber, Accenture, IBM, and many startups. 

Placed Students

Our Clients

Partners

...

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses