DEV Community

Mariam Adeyemi
Mariam Adeyemi

Posted on

How To Set Up An Express Server in a Nodejs Project

Node.js is an open-source server environment powered by JavaScript. It is an excellent choice for JavaScript developers and diehard JavaScript fans who do not want to switch to another language to program the backend of an application.

Express is a lightweight and adaptable Node.js framework that offers a comprehensive set of features for developing web and mobile applications. It makes setting up a node server easier and faster.

Setting up an express server is incredibly easy and straightforward. We are going to do it in a few steps and with even less code.

Step One: Install Node

The first step in this project is to install Node.js. Don't forget that this is a node.js project so this is an important part of our project that cannot be skipped. Head to the node website to download the latest version of node. You can skip this step if you have it already installed or run npm -v in the terminal to check the version of the node you have on your operating system.

Step Two: Install Express

Now let's install express into our project. Create a new folder for your project then open the folder in VSCode or any code editor/IDE of your choice. Create a new js file, you can name it "server.js". Open the terminal and run the command:

npm install express

This command will initialize npm and install the necessary packages and dependencies. After it has been successfully installed, you should have three new files. A node module folder and two JSON files. If you check the package.json file, you will see something like this

{
  "dependencies": {
    "express": "^4.18.2",
   }
}
Enter fullscreen mode Exit fullscreen mode

This tracks the dependencies your project has access to. In the next step, we are going to add a new dependency and it will show in this file.

Recommended Step: Install Nodemon

This step is optional but necessary for efficiency. Nodemon helps to restart the server every time you save any changes. It prevents the tiresome process of restarting it yourself every time. So, let's go back to our terminal to install nodemon. Run the command:

npm install -g nodemon

The -g tag means globally and it enables you to use nodemon in all your projects, not just the current one.

Step Three: Require Express

Let's go back to our server.js file to finish setting up our express server. The first thing to do is to require express:

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

Enter fullscreen mode Exit fullscreen mode

The above code required the express package and assigned the express function to a constant named "app". The variable name can be anything but it is conventional to name it like that.

Step Four: Set Up Port

Next, we going to set up our port to run our server locally.

const PORT = 3000;
app.listen(PORT, ()=>{
   console.log(`Server is running at http://localhost:${port}`)
})
Enter fullscreen mode Exit fullscreen mode

The above code saved the port to a variable and called the "listen" method from our express framework. The listen method takes a single argument and an optional callback function. We passed the port number as the argument and called a function that will log something to the console once the server is set up successfully.

Step Five: Run It

Run the command nodemon server.js in the terminal and if there is no mistake in your code, you should see the console.log message and your server running at the implied port. Congratulations! you have successfully set up your express server in a few steps.

Bonus: Hello World

If we visit our port address in the browser, the page is going to show up blank. It is because our newly set up server is not sending any response for the page to display. We can solve this by routing. Let's make a simple GET request to the home route and send back a "Hello World" response.

Warning! The position of the following code is very important, you should make sure it comes after our first two codes that initialized the express framework and before the port listening code.

app.get("/", (req, res)=>{
   res.send("Hello World");
})
Enter fullscreen mode Exit fullscreen mode

The code makes a GET request to the home route which is represented with "/" and sends a string ("Hello World") as a response. The get method takes an argument and a callback function. The callback function has two parameters: the request and response. We will talk more about routing and other concepts in later posts. Stay tuned for more.

Until next time.

Latest comments (0)