Posted on
Web Development

Setting up Express.js for backend development

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Comprehensive Guide to Setting Up Express.js for Backend Development on Linux Bash

In the world of web development, having a robust framework can drastically reduce the time and effort required to build server-side applications. Express.js stands out as a minimalist and flexible node.js web application framework, providing a strong set of features to develop both web and mobile applications efficiently. In this guide, we will walk through the process of setting up Express.js for backend development on a Linux Bash environment.

Prerequisites

Before we get into setting up Express.js, ensure that your Linux environment is ready with the following prerequisites:

  1. Node.js: Express is a framework for Node.js, hence Node.js needs to be installed.
  2. NPM (Node Package Manager): Used for managing Node.js packages.
  3. A text editor or IDE: Such as Visual Studio Code, Atom, or similar for coding.

Check if Node.js and NPM are installed by running the following commands in your Linux Bash:

node -v
npm -v

If these commands don't return version numbers, you will need to install Node.js and NPM.

Installing Node.js and NPM on Linux

You can install Node.js and NPM via the package manager of your Linux distribution. For Ubuntu-based distributions, use the following commands:

sudo apt update
sudo apt install nodejs npm

For RHEL, CentOS and Fedora distributions, use dnf (modern Fedora and RHEL/CentOS 8+):

sudo dnf install nodejs npm

For older versions of RHEL or CentOS (6 or 7), you might still need to use yum:

sudo yum install nodejs npm

For SUSE-based distributions, use zypper:

sudo zypper install nodejs npm

Once installed, verify the installation again with the node -v and npm -v commands.

Setting up Express.js

Step 1: Initialize a New Node.js Project

First, create a new directory for your project and navigate into it:

mkdir my-express-app
cd my-express-app

Inside the project directory, run the following command to create a package.json file:

npm init -y

Step 2: Install Express.js

Install Express within your project directory using npm:

npm install express

This command installs Express and adds it as a dependency in your package.json file.

Step 3: Create Your First Express Server

Create a new file called app.js in your project directory:

touch app.js

Open app.js using a text editor and add the following code to set up a basic Express server:

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

app.get('/', (req, res) => {
  res.send('Hello World!');
});

const port = 3000;
app.listen(port, () => {
  console.log(`Server running on http://localhost:${port}`);
});

Step 4: Run the Server

Start your server using Node.js:

node app.js

Navigate to http://localhost:3000 in your web browser. You should see "Hello World!" displayed.

Expanding Your Express.js Application

Now that you have a basic server running, you can start adding more routes, middleware, and even connect to databases:

Adding Routes

You can define additional routes by using the app.get, app.post, app.put, etc., methods to handle different HTTP requests:

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

Using Middleware

Express is powerful when it comes to using middleware. Here’s how you could log requests:

const logger = (req, res, next) => {
  console.log(`${req.method} ${req.url}`);
  next();
};

app.use(logger);

Connecting to a Database

Integrate a database like MongoDB using Mongoose:

npm install mongoose

Then, you can use it within your app to connect to your database and define schemas.

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true });

Conclusion

Setting up Express.js on a Linux Bash environment is straightforward and allows you to build powerful backend services quickly and efficiently. Use the extensive middleware support and comprehensive routing capabilities to expand your application according to your project's needs. Happy coding!

This guide covers the basics to get you started with Express.js in a Linux environment. For more detailed requirements, consult the Express.js and Node.js documentation as you advance.

Further Reading

For those interested in diving deeper into backend development with Express.js, consider exploring these resources:

  • Express.js Official Documentation: A complete reference to all features and settings https://expressjs.com/

  • Learn about Middleware in Express.js: Gain a deeper understanding of middleware and its usage in Express applications https://www.tutorialspoint.com/expressjs/expressjs_middleware.htm

  • Node.js Official Website: Explore more about Node.js, upon which Express.js is built https://nodejs.org/

  • Handling API Authentication in Express: Techniques for securing your Express.js applications http://www.passportjs.org/docs/

  • Connecting Express.js with MongoDB: In-depth tutorial on using MongoDB with Express for building full-stack applications https://mongoosejs.com/docs/guide.html