Requesting in HTTP vs Requesting KoaJS
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.
http Module
Import http module: Import http module and store returned HTTP instance into a variable.
Syntax:
var http = require("http");
Creating and Binding Server: Create a server instance using createServer() method and bind it to some port using listen() method.
Syntax:
const server = http.createServer().listen(port)
Parameter: This method (listen()) accepts a single parameter as mentioned above and described below:
Below example illustrate the use of http module in Node.js.
Example 1: Filename: index.js
const http = require( "http" );
const PORT = process.env.PORT || 2020;
const server = http.createServer(
function (req, res) {
res.write( 'Hello geeksforgeeks!' );
res.end();
}
)
.listen(PORT, error => {
console.log(`Server listening on port ${PORT}`)
});
|
Run index.js file using the following command:
node index.js
Output:
Server listening on port 2020
Now type http://127.0.0.1:2020/ OR http://localhost:2020 in a web browser to see the output.
Koa Module
In order to use the Koa module, we need to install the NPM (Node Package Manager) and the following modules (on cmd).
>> npm init // Creates package.json file
>> npm install koa --save // Installs express module
>> 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
const koa = require( 'koa' );
const app = new koa();
const PORT = process.env.PORT || 2020;
const IP = process.env.IP || 2021;
app.use( function *() {
this .body = "Hello GeeksForGeeks!" ;
});
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.
Share your thoughts in the comments
Please Login to comment...