Skip to content

1. ExpressJS

1. ExpressJS vs NodeJS

NodeJS: Node JS is an open-source and cross-platform runtime environment for executing JavaScript code outside of a browser. You need to remember that Node JS is not a framework and it’s not a programming language. Most of the people are confused and understand it’s a framework or a programming language. We often use Node.js for building back-end services like APIs like Web App or Mobile App. It’s used in production by large companies such as Paypal, Uber, Netflix, Walmart, and so on.

ExpressJS: Express is a small web framework that sits on top of Node JS’s web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middleware and routing. It adds helpful utilities to Node JS’s HTTP objects. It facilitates the rendering of dynamic HTTP objects.

  • Node JS is a platform for building i/o applications that are server-side event-driven and made using JavaScript.
  • Express JS is a framework based on Node JS which is used for building web applications using approaches and principles of Node JS event-driven architecture.
FeatureExpress JSNode JS
UsageIt is used to build web-apps using approaches and principles of Node JSIt is used to build server-side, input-output, event-driven apps.
Level of featuresMore features than Node JS.Fewer features.
Building BlockIt is built on Node JSIt is built on Google’s V8 engine.
Written inJavaScriptC, C++, JavaScript
Framework/PlatformFramework based on Node JSRun-time platform or environment designed for server-side execution of JavaScript.
ControllersControllers are provided.Controllers are not provided.
RoutingRouting is provided.Routing is not provided.
MiddlewareUses middleware for the arrangement of functions systematically server-side.Doesn’t use such a provision.
Coding timeIt requires less coding time.It requires more coding time.

🏗 The MVC Concept

Express apps often follow the MVC (Model-View-Controller) pattern:

  • Model – Data logic (e.g., database access)
  • View – Presentation (HTML templates)
  • Controller – Application logic (routes & middleware)

2. npm vs Node.js

Both Node.js and NPM have their own command-line interfaces. The Node.js CLI allows developers to execute JavaScript code on the server side, while the NPM CLI is used for package management tasks such as installing and updating packages.

The NPM CLI is built on top of the Node.js CLI and provides additional functionality for managing packages and dependencies.

3. Environment Setup

Create a folder and issue the following command inside the folder

Terminal window
npm init -yes

Will create a package.json file inside the folder

{
"name": "testproject",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

3.1 Install ExpressJS

Play

Crash Course of ExpressJS

Advance Course of ExpressJS

Terminal window
npm install express

To avoid restarting the server every time on changes to the project

Terminal window
npm install nodemon

Installation of packages will append dependency section to the package.json file

package.json
{
...
"dependencies": {
"express": "^4.19.2",
"nodemon": "^3.1.0",
}
}

Modify the package.json file and add two lines in "scripts"

package.json
"scripts": {
"start": "node index.js",
"devStart": "nodemon index.js"
}

3.2 Create index.js File

index.js is the main file which will be executed by node.

index.js
const express = require('express');
const app = express()
app.get('/' , function(req, res){
res.send("Hello World")
});
// starts the server and tells it to listen for incoming connections on port 3000.
app.listen(3000, function(){
console.log('Listening on port 3000')
});
  • app.get('/', ...) sets up a GET request handler for the root path (/) of your website.
  • function(req, res) {...} is the callback that runs when someone visits /.
    • req = the request object (info from the client)
    • res = the response object (used to send a reply)

3.3 Start node server

Terminal window
node index.js
or
npm run start
or
npm start # works only for predefined scripts in npm

Start server with nodeman

Terminal window
nodemon index.js
or
npm run devStart

Restarting of the server is not required every time the code is changed.

3.4 🧰 Useful npm Packages

  • morgan – Logging
  • dotenv – Load environment variables
  • cors – Enable cross-origin requests
  • helmet – Secure headers
  • body-parser – Parse incoming request bodies