Building a simple Express server in Node.js for Beginners

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

NodeJS (Express) - How to Write Express Middleware

Learn how to write Express middleware. Middleware in Express is one of the easiest ways to create DRY. What Is Middleware? Middleware is an often misunderstood topic since it sounds and appears very complicated, but in reality middleware is actually really straightforward.

Remote fetching HTML data and Parse It in Express for NodeJS

Remote fetching HTML data and Parse It in Express for NodeJS - Have you ever wanted to fetch remote HTML data in a similar fashion to cURL or Wget using some backend web service and parse it? If you said no, don’t worry, I said the exact thing until I was challenged to do so. For the challenge, I chose to use Express as my framework and a few libraries to assist in the process of fetching and parsing.

Portfolio Site With Node JS & Express - Full

This tutorial will cover: Project Structure, Templating Engine, Request Routing, REST API, CRUD operations File uploading, CMS for blogs and projects, AJAX with jQuery. Let's dive into Portfolio Site With Node JS & Express - Full

Node.js REST API with Express & MongoDB - Getting Started with Express

Node.js REST API with Express & MongoDB - Getting Started with Express. Learn Node.js framework in simple and easy steps starting from basic to advanced concepts with examples including Introduction, Environment Setup, Node Package Manager, Node Callbacks Concept, Node Buffers Module, Node Streams, Node File System, Node Utility Modules, Node Web Module, Node Express Application, Node RESTFul API, Node Scaling Application.

Creating an Image Upload App using NodeJS & Express

Creating an Image Upload App using NodeJS & Express - This is just a speed code of a simple prototype for an image uploading application. This is my first time creating this and decided I would show you guys my thought process and what goes on in my head when I try to implement new ideas!