In this post, we’ll lay the foundation for our MERN backend stack by setting up Node.js and Express. Node.js is a JavaScript runtime that allows us to run JavaScript on the server-side, while Express is a minimalistic and flexible web application framework for building APIs and handling HTTP requests.
The first step is to install Node.js on your machine. You can download the latest version of Node.js from the official website: https://nodejs.org/en/download/
Follow the installation instructions for your operating system. Once installed, you can verify the installation by opening a terminal or command prompt and running the following command:
node --version
This should print the installed version of Node.js. Additionally, you can check the version of npm (Node Package Manager) by running:
npm --version
npm is a package manager that comes bundled with Node.js and allows us to install and manage third-party packages and dependencies.
Next, let’s create a new directory for our project and initialize a new Node.js project using npm:
mkdir mern-backend # Create a new directory
cd mern-backend # Change directory
npm init -y # Initialize a node project
The npm init -y
command creates a new package.json
file in the current directory. This file contains metadata about your project, including dependencies and scripts.
{
"name": "mern-backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
With Node.js set up, we can now install Express, which will serve as the backbone of our backend application. Enter the following command in the terminal:
npm install express
This command installs the latest version of Express and adds it as a dependency in your package.json
file.
Now that we have Express installed, let’s create a new file called server.js
in the project root and add the following code:
const express = require("express");
const app = express();
const port = 3000;
app.get("/", (req, res) => {
res.send("Hello, World!");
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Here’s what this code does:
3000
) for our server to listen on./
) using app.get()
. When a client makes a GET request to this path, the server will respond with the string “Hello, World!“.app.listen()
.To run the server, open a terminal, navigate to your project directory, and execute the following command:
node server.js
You should see the message “Server is running on port 3000” printed in the terminal. Open your web browser and navigate to http://localhost:3000
, and you should see the “Hello, World!” message displayed.
You can add the command node server.js
as a script in the package.json
file.
"scripts": {
+ "start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Now, instead of using node server.js
, you can run npm run start
to start the server. This becomes handy when the process to start the server involves more than just running a file with node. For example, the command corresponding to the start
script can abstract a more complex process, such as running a few different services or seeding the database.
During the development process, it can be quite a chore to manually restart the server every time you make changes. This is where Nodemon comes in handy. Nodemon is a utility that will monitor for any changes in your source and automatically restart your server. This can be a huge time saver during development.
To install Nodemon, you need to run the following command in your project directory:
npm install nodemon --save-dev
The --save-dev
flag ensures that Nodemon is installed as a development dependency, which means it’s only required during the development phase and not for running the application in production.
With Nodemon installed, let’s update the scripts in our package.json
file to use Nodemon for running our server in development mode:
"scripts": {
+ "dev": "nodemon server.js"
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Now, you can start your server in development mode using the following command:
npm run dev
With this setup, your server will restart automatically whenever you make changes in your server.js
file, making your development process smoother and more efficient.
Congratulations! You’ve successfully set up a basic Node.js and Express server. In the next post, we’ll explore how to integrate TypeScript into our project and convert our existing code to take advantage of TypeScript’s features.