Building a simple Express server in Node.js for Beginners

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. In this post, I will show you how to set up a simple node.js express server, and other basics that you need to know about it.

Table of Contents

1 #Create package.json file
2 #Install the express package
3 #Create .env file
4 #Set up the server.js file
5 #Run the express web server

Getting Started

1. Create package.json file

First we will create our project directory and our package.json. Open up your terminal and go to your project directory. In the terminal run the following command

mkdir webserver
cd webserver
npm init

When you run npm init choose the default options except for the entry point type server.js.

2. Install the express package

Now you should see a package.json file in your directory. Next we will install some packages to setup our server. Run the following command in your terminal.

npm i express dotenv


This will install the express library and the dotenv module. Dotenv is a module that will load environment variables from .env files.

We are also going to install a package called nodemon. This is a module that will restart our server everytime we make a change.

npm i --save-dev nodemon

Once those packages are installed we will go to our package.json under the "scripts" object. We will remove what is inside that "scripts" object so that it looks like the following.


"scripts": {
    "dev": "nodemon server"
}

Now when we want to run our server we could type in our terminal npm run dev and it will run the nodemon server.js for us

3. Create .env file

Next, we will create our .env file. In your project directory create a folder named config and inside that folder create a file called config.env. You can create the folder through your terminal or text editor.

mkdir config
cd config
touch config.env

Your project directory should look like the following

-config
--config.env
-node_modules
-package-lock.json
-package.json

In the config.env file we will create two environment variables. The first one will be a NODE_ENV variable. We will use this variable to determine whether the server is running in production or development. The second variable will be PORT. This variable will determine what port the server is running on.


NODE_ENV=development
PORT=5000

4. Set up the server.js file

Now we will set up our server.js file. In this file import the libraries we installed to set up our server. Lets first create the server.js file.

touch server.js


Now lets import express and dotenv in the file.

// server.js
const express = require('express');
const dotenv = require('dotenv');

Next, we will configure the dotenv loader.

// server.js
const express = require('express');
const dotenv = require('dotenv');

dotenv.config({ path: './config/config.env' });

We will then create two variables. The first will be called PORT. We will set it equal to our PORT environment variable. The second will be app which will be set equal to the express variable being invoked.

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

dotenv.config({ path: './config/config.env' });

const PORT = process.env.PORT;

const app = express();

Lastly, we will take the app variable and run the listen() method attached to it to run the server.


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

dotenv.config({ path: './config/config.env' });

const PORT = process.env.PORT;

const app = express();

app.listen(PORT, console.log(`Server listening on port ${PORT}`));

5. Run the express web server

Let’s go ahead and run the web server with the following command

npm run dev


Now you should see in the terminal the following message


Server listening on port 5000

Conclusion

You have now created a simple node.js express server. If you navigate to localhost:5000 you will see a message saying Cannot Get /.

#node #nodejs #express #javascript

Building a simple Express server in Node.js for Beginners
2 Likes39.60 GEEK