Express.js vs KoaJS in Node.js
Last Updated :
14 Sep, 2020
Node.js: Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS 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.
Express Module: In order to use the express module, we need to install the
NPM (
Node Package Manager) and the following modules (
on cmd).
// Creates package.json file
>> npm init
// Installs express module
>> npm install express --save // OR
>> npm i express -s
Import express module: Import express module and store returned instance into a variable.
Syntax:
var express = require("express");
Creating Server: The above syntax calls the "express()" function and creates a new express application which gets stored inside the app variable.
Syntax:
// Importing and creating express application
const app = express();
var express = require("express")(); // OR
Sending and listening to the response: It communicates the request and response with the client and the server. It requires PORT <number> and IP <number> to communicate.
app.listen(PORT, IP, Callback);
Parameter: This method accepts three parameters as mentioned above and described below:
-
PORT <Number>: Ports are the endpoints of communication which helps to communicate with the client and the server.
-
IP <Number>: IPs represent IPv4 or IPv6 address of a host or a device.
Callback <Function>: It accepts a function.
The below example illustrates the Express.js module in Node.js.
Example 1: Filename: index.js
javascript
// Node.js program to create server
// with help of Express module
// Importing express
const express = require('express');
// Creating new express app
const app = express();
// PORT configuration
const PORT = process.env.PORT || 2020;
// IP configuration
const IP = process.env.IP || 2021;
// Create a route for the app
app.get('/', (req, res) => {
res.send('Hello Vikas_g from geeksforgeeks!');
});
// Create a route for the app
app.get('*', (req, res) => {
res.send('OOPS!! The link is broken...');
});
// Server listening to requests
app.listen(PORT, IP, () => {
console.log(`The Server is running
at: http://localhost:${PORT}/`);
});
Run index.js file using the following command:
node index.js
Output:
The Server is running at: http://localhost:2020
Now type http://127.0.0.1:2020/ OR http://localhost:2020/ in a web browser to see the output.
KoaJS Module: In order to use the
KoaJS module, we need to install the
NPM (
Node Package Manager) and the following modules (on cmd).
// Creates package.json file
>> npm init
// Installs koa module<
>> npm install koa --save
>> npm i koa -s // OR
Import KoaJS module: Import KoaJS module and store returned instance into a variable.
Syntax:
var koa = require("koa"); // Importing koa module
Creating Server: The above syntax imports the koa module and creates a new koa application which gets stored inside the app variable.
Syntax:
const app = new koa(); // Creating koa application
Sending and listening to the response: It communicates the request and response with the client and the server. It requires PORT <number> and IP <number> to communicate.
app.listen(PORT, IP, Callback);
Parameter: This method accepts three parameters as mentioned above and described below:
-
PORT <Number>: Ports are the endpoints of communication which helps to communicate with the client and the server.
-
IP <Number>: IPs represent IPv4 or IPv6 address of a host or a device.
-
Callback <Function>: It accepts a function.
The below example illustrates the KoaJS module in Node.js.
Example 2: Filename: index.js
javascript
// Node.js program to create server
// with help of Koa module
// Importing koa module
const koa = require('koa');
// Creating new koa app
const app = new koa();
// PORT configuration
const PORT = process.env.PORT || 2020;
// IP configuration
const IP = process.env.IP || 2021;
app.use(function *() {
this.body = "Hello GeeksForGeeks!";
});
// Server listening to requests
app.listen(PORT, IP, ()=>{
console.log("Server started at port", PORT);
});
Run index.js file using the following command:
node index.js
Output:
The Server is running at port 2020
Now type http://127.0.0.1:2020/ OR http://localhost:2020/ in a web browser to see the output.
Similar Reads
HapiJS vs KoaJS in Node.js HapiJS Module: In order to use the hapiJS module, we need to install the NPM (Node Package Manager) and the following modules (on cmd). // Create package.json file >> npm init // Installs hapi module >> npm install @hapi/hapi --save Import hapiJS module: Import hapiJS module and store re
3 min read
Node vs Express Node.js and Express are two essential tools in the JavaScript ecosystem, especially for server-side development. While they are often used together, they serve different purposes. This article will explain what Node.js and Express are, their key differences, and when to use each.Table of Content Nod
5 min read
What are modules in Node JS ? In NodeJS, modules are encapsulated units of code that can be reused across different parts of an application. Modules help organize code into smaller, manageable pieces, promote code reusability, and facilitate better maintainability and scalability of NodeJS applications. Types of Modules:Core Mod
2 min read
Unique features of Express JS Express.js is a lightweight and powerful web framework built for Node.js. It simplifies building web applications and APIs by providing essential features that help developers create scalable, high-performance applications easily. Let's explore the unique features of Express.js that make it a popula
5 min read
What is the use of next() function in Express.js ? Express.js is a powerful framework for node.js. One of the main advantages of this framework is defining different routes or middleware to handle the client's different incoming requests. In this article, we will discuss, the use of the next() function in every middleware of the express.js.There are
2 min read