January 8, 2020 Adem Bilican No comments

Creating a simple back-end with ExpressJS

Today’s topic is ExpressJS, a web framework for NodeJS. You can use ExpressJS if you need any back-end program, for example for a web-app or a REST API. In this article, I will take you through the installation of ExpressJS and the creation of a very simple project.

1. Creating an empty nodeJS project

Let’s start with the installation. Open your terminal and create a new directory:

mkdir back-end
cd back-end

We then need to initialize a new node project, we will use the yarn package manager (you can also use npm of course)

yarn init

Answer the questions to finalize the initialization of the node project. You will see a new package.json file in your directory, that’s a good sign!

2. Installing the required packages

Now, let’s install the libraries we need:

  • nodemon: to help us automatically refresh and restart the server on any file change, very helpful to avoid stopping and restarting the server manually every time!
  • express: the NodeJS web framework
  • cors: to provide the required Middleware for Express to allow the CORS mechanism (to put in simple words: cors allows the communication between different servers)
  • body-parser: middleware to parse the request body object and give access to it via req.body
  • morgan: logger for nodeJS

Run the following commands to install all the packages:

yarn global add nodemon
yarn add express
yarn add cors
yarn add body-parser
yarn add morgan

Next, we will create the index.js file that will be called on npm start.

3. Implementing index.js

Run the following command on your terminal to create an empty index.js file

touch index.js

Add the following code to your index.js file (I have added more information as comments in the code)

// import all the required packages
const express = require("express");
const app = express();
const cors = require("cors");
const bodyParser = require("body-parser");
const logger = require("morgan");
// define port number
const port = process.env.PORT || 3001;

// instantiate express with the correct parameters
app.use(logger('dev'));
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// create a Router
const router = express.Router();

// example of a GET method that returns a simple "Hello World"
router.get('/', (req, res) => {
  res.send( "HELLO WORLD" );
});

// define the router to use
app.use('/', router);

// start express
app.listen(port, function() {
    console.log("Runnning on " + port);
});

module.exports = app;

4. Starting our server

Finally, start your webserver by running

nodemon index.js

You can now access the page returned by your web-server via the following URL:
localhost:3001
You should see the “Hello World” text returned by your server! Well done!
Now it’s time to add more routes to your Router with the following functions:

router.get('/', (req, res) => { });
router.post('/', (req, res) => { });

Have fun 😉


We can work together

Photo by unsplash-logoTaylor Vick on Unsplash

Leave a Reply

Your email address will not be published. Required fields are marked *