How to get started with NodeJS and Express

How to get started with NodeJS and Express

ยท

3 min read

Let's build a webserver using express

node.js

Node.js is an open source, cross-platform javascript runtime environment focusing on serverside and networking applications.

express.js

Express.js is a small framework that works on top of a node.js web server functionality to simplify its APIs and add helpful new features It makes organizing your application's functionality easier with middleware and routing. It adds helpful utilities to node.js HTTP objects and facilitates rendering dynamic HTTP objects.

Project setup

  1. Open the terminal window and create a new project directory

     mkdir myProject
    
  2. Enter into the newly created directory

     cd myProject
    
  3. Initialize a new npm project

     npm init -y
    
  4. Install express

     npm install express
    
  5. Open your code editor

Create an express server

Now that express is installed, create a new index.js file and app.js file and add the following lines of code

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

The first line is grabbing the express module from the installed package. In the second line, we create an app variable and assign the express module function to it.

Now let's create the home route

// app.js

app.get("/", (req, res) => {
    res.send("<h1>App is running successfully</h1>");
});
module.exports = app;

These lines of code tell the express server how to handle a get request to our server.

The get is a function that has two main parameters, the first one is the URL (here we are targeted "/" which is the root of our website, and the second parameter is a function with two arguments, the req, and res. We can create multiple routes this way each with its requests and responses.

req is the request that we sent to the server

res is the response that we send back to the client

Then wrap the whole module and export with the name called app and import it into our index.js file

//index.js
const app = require("./app");

What is an environment variable?

Environment variables are set outside a program, often through a cloud provider or operating system.

In node.js, environment variables are a great way to securely configure things that don't change often like URLs, authentication keys, and passwords

Creating environment variables in our app

To create an environment variable in our node app, we will want to use dotenv package.

dotenv is a lightweight npm package that automatically loads environment variables from a .env file into a process.env object.

To use dotenv, install it using the below command.

npm install dotenv

Create a file called .env at the top level of your file structure.

Let's set PORT variables to 4000 in the .env file

// .env
PORT = 4000

Bring the dotenv inside the app.js

// app.js

require("dotenv").config()
const express = require("express");
const app = express();

app.get("/", (req, res) => {
   res.send("<h1>App is running successfully</h1>");
});
module.exports = app;

Access the environment variable from the index.js file

const app = require("./app");
const { PORT } = process.env;
app.listen(PORT, () => "Server is running at 4000");

Finally, it's time to start our server. We are passing 4000 into the listen function, which tells the app which ports to listen on. The function passed in as the second parameter is optional and runs when the server starts up giving us feedback in the console to know that our application is running.

Now it's the time to run our application

node index.js

output

Thank you ๐Ÿ™‹

ย