DEV Community

Cover image for Up and Running With NodeJS Express App In A Minute (2022)
Raja Tamil
Raja Tamil

Posted on • Originally published at softauthor.com

Up and Running With NodeJS Express App In A Minute (2022)

In this Nodejs tutorial, you’re going to learn how to learn how to get up and running with nodejs app in a minutes.

Let’s get started.

Install Node.js & NPM

First make sure you install node and npm in your computer if you haven’t already.

Go to nodejs.org and download the LTS version of it in your computer.

alt text

Once its downloaded, double click to complete the installation process.

At this stage you’ve successfully installed Node as well as NPM (Node Package Manager).

To verify that, open up the Terminal or Command prompt from you computer and run the following commands:

node -v
npm -v
Enter fullscreen mode Exit fullscreen mode

Initialize Your Project

Create a project folder in your computer and open it up in your favourite editor. I prefer visual studio as it comes with Terminal.

Then open up the Terminal Window, make sure you’re in the project directory, and run the following command which will initialize our project by creating a package.json file inside your project folder.

npm init -y
Enter fullscreen mode Exit fullscreen mode

Install Express

The next step is to install express, which is one of the most popular Node.js web application frameworks, so that we don’t have to get started from scratch.

To install express:

npm I express
Enter fullscreen mode Exit fullscreen mode

Once it’s installed successfully, you can see a folder called “node_modules” and a file called “package-lock.json” added to the project folder.

You can also check for the installed npm packages inside package.json under dependencies object.

alt text

Install Nodemon

Nodemon is one of the npm packages that will help us to run the app without restarting the server every single time when we make a change in our project.

To install nodemon, run the following command:

npm install nodemon
Enter fullscreen mode Exit fullscreen mode

Then go to package.json and replace the property inside the scripts from:

Test: echo \”Err
Enter fullscreen mode Exit fullscreen mode

to:

"devStart" : "nodemon index.js"
Enter fullscreen mode Exit fullscreen mode

Create Index.js file

Inside the project, create an index.js file, in there, import express at the top and create an express server and store it in a constant called app.

const express = require('express');
const app = express();
Enter fullscreen mode Exit fullscreen mode

Then, create a route for a home url which is “/” using HTTP get() request method.

app.get("/", (req, res) => {
    res.send("I will be shown on the Browser");
    console.log("I will be shown on the Terminal");
});
Enter fullscreen mode Exit fullscreen mode

Get() method takes two argument:

  1. URL route, in this case, home url (“/”).
  2. The callback arrow function which will have req and res as the parameters. This function will be invoked when a user goes to the home route.

Then, we can send some data to the browser using the response object by invoking send() method on it.

The text we’ve added as an argument to the send() method will be visible on the browser.

I’ve also console logged so that we can also see the text in our terminal/command line.

Finally, listen for port 3000.

app.listen(3000);
Enter fullscreen mode Exit fullscreen mode

Run Node.js App

Let’s run the app by using the following command:

nodemon
Enter fullscreen mode Exit fullscreen mode

Sometimes, you may get an error saying nodemon app crashed – waiting for file changes before starting.

If you do get such an error, run the following command and restart nodemon

killall -9 node

And that should fix the issue.

Once nodemon is running successfully, you can see the console.log message appears on the Terminal window.

alt text

Open up the browser and navigate to the following URL:

http://localhost:3000/
Enter fullscreen mode Exit fullscreen mode

You can see the text appears on the browser.

alt text

Top comments (0)