Node

Create Your First Node js app with Express – Step by step guide 2019

In this article, I will provide a detailed step by step guide on how to create your first Node js app with...

Written by admin · 3 min read >
first-node-express-app

In this article, you will learn how to create your first Node.js app with Express. I will provide a step by step detailed guide with examples and a required screenshot of code which will help you to understand the code and concepts.

Here we will start our discussion by building an understanding of Nodejs concepts like what is node.js? How does node.js work? its benefits and then create our app. So Let’s start with some important concepts of Node – Express app with an introduction of Node.js.

Node.js: An Introduction

Node.js is an open-source, cross-platform, runtime environment that enables developers to write server-side applications in javascript.

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine.

Nodejs.org

Node.js allows building scalable server-side applications using JavaScript. We can develop various types of applications like Command line applications, Web applications, REST API and Web server, etc.

How Node.js works? : Behind the scene

Javascript runs in the browser. The browser’s engine like Chrome’s V8, Mozilla’s SpiderMonkey, IE’s Chakra takes JavaScript code and compiles it into the machine code. The creator of Node.js, Ryan Dahl, took Chrome’s V8 engine and built a runtime for it to work on a server.

The runtime is basically enabled us to use javascript outside of a browser context (i.e. running directly on a computer or server OS). It’s an environment where the language can get interpreted.

Why Node.js : Benefits of using Node.js

  • Node.js is Event-driven, non-blocking (asynchronous) I/O, Non-blocking execution enables it one of the fastest language for building web applications today.
  • It can handle many requests concurrently using Event Loop, for each connection, the callback is fired, but if there is no work to be done Node will remain asleep.
  • Asynchronous or non-blocking means, handling events without interrupting the main thread.
  • Great performance by optimizing throughput and scalability in the web app and also a good solution for real-time web applications.
  • As Javascript is on both sides, client-side and server-side allows less time spent dealing with context shifts between languages.
  • The node package manager (NPM) provides access to many of reusable packages. It also has best-in-class dependency resolution.
event loop in nodejs

Prerequisites: Before creating the Node.js app

  • If you are aware of Node.js concepts, It will be better for you.
  • If not, a basic javaScript knowledge will help you to understand the code.
  • A terminal app for MacOS and PowerShell/Command prompt for Windows.
  • Node.js v8+ and a Node.js package manager installed locally.
  • You can check Node.js and NPM version by using the following command :
  • node -v && npm -v
nodejs & npm version check
How to check Node.js and NPM version

So let’s create your first Node js and Express app – step by step

Step1: Install Node.js

To install Node.js and NPM, use any of the official Node.js installers provided for your operating system.

Installing Node.js
Download and Install Nodejs

Download the Nodejs installer by clicking the install button for the LTS (long-term-stable) version. It’ll detect your OS and give you the appropriate installer. Run the installer. That’s it, you have installed Node.js. Now check the version using the command node -v && npm -v

Install Node.js on Mac using Brew

Install Homebrew (Mac package manager), if not yet installed

//install brew
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

//install node
brew install node

Step 2: Initialize Node app – Role of Package.json

Assuming you’ve installed Node.js on your machine, Now create a working directory to hold your application and init the nodejs app.

Run command npm init, It will create a package.json file for your application in your working directory. This command prompts you for a number of things, including the name and version of your application and the name of the initial entry point file (by default this is index.js). For now, just accept the defaults.

npm init
//package.json
{
  "name": "first-node-app",
  "version": "1.0.0",
  "description": "my first nodejs express app",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Step 3 – Install Express.js

Express.js is the most popular middleware choice when it comes to building web applications with Node.js. It is a web application framework, enables you to build server applications in Node.js

Express JS is Fast, unopinionated, minimalist web framework for Node.js

expressjs.com

Install Express using command:

npm install express --save

The –save command tells npm to add the packages as dependencies in the package.json. By default with npm v5.0+, npm install adds the module to the dependencies list in the package.json file. In earlier versions of npm require the –save flag to be specified explicitly.

install express js
npm install express –save

Add "start": "node index.js" code to scripts, If it is not there.

//package.json
{
  "name": "first-node-app",
  "version": "1.0.0",
  "description": "my first nodejs express app",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

Step 4: Write Node.js code

Add below code to your index.js

const express = require('express');
const app = express();
 
app.get('/', (req, res) => {
  res
    .status(200)
    .send('Hello, world!')
    .end();
});
 
// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});

Step 5: Run your Node.js application

Let’s start application locally. Simply start it with the following command:

npm start

If you have not added "start": "node index.js" code to scripts. Still you can start the application by the following the command:

node index.js //entry point js file

Once you start your application on the command line with npm start, you should be able to see the output in the command line:

npm start
npm start
Check your first Nodejs app on localhost:8080

Congratulations !! you’ve completed the first step in your Node-Express journey. From here now you can start learning Nodejs and Express js main benefits, and create first REST API, for that you should understand the concepts of Express routes, middleware, error handling, and template code.

In this article, I have provided a base for your next steps toward Nodejs. I hope you liked this article then don’t forget to share it with others. Happy coding…

Leave a Reply

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