Quick Contact


    Express Framework in Node JS

    When you want to make website in JavaScript, basically developer use JavaScript everywhere like front end, back end, database and Framework as well. When you say front-end you have all used JavaScript in the front-end then you also use JavaScript in the back-end. When it comes to JavaScript, when it comes to Node JS the web framework you are going to use here is Express JS. So why you need a framework?

    Node JS provides you http module using which you can create a website where user will send request and you have to send a response that works but when you say you want to build web application it will not be having the simple request response thing. You build a website where you have a multiple type of requests. To handle every type of request you need good framework and Express.js is an amazing framework. It is fast and it provides you minimum features so that you can build an amazing website. You should have latest version of Node JS.

    You should also install “npmi express” differently to run project in Express Framework. With Express you will many other modules. There are definitely some dependencies are there. For you Expresso is a dependency and for Express all modules are dependencies.

    Let initialize with Express-:

    Const express = require (‘Express’);

    Const app = express ();

    So move forward with express. As you request for some website or you request for some URL, run on localhost with portable 8000, then it should return “Hello world” so code will be as-:

    	Const express = require (‘express’);
    	Const app = express ();
    	App.get (‘/’, function (req, res)(
    	Res.send (‘hello world’)
    	})
    	App.listen (9000, function (req, res) {
    	Console.log (‘Running...’)
    	});
    	
    Express Framework-in-nodejs

    So you have achieved one thing, but then you also wanted to have this thing right when you request for alien, it should give you data about alien so how do you do that and for that you can create a separate function so you can say

    	app.get (‘alien’, function (req, ) {
    	res.send (‘welcome back alien’)
    	})
    	
    So code will be as-:
    Express Framework-in-nodejs
    Express Framework-in-nodejs

    That’s how you specify the routing so for different url’s you will have different functions which is working for it. But if you want, if you pass a particular alien by saying 20 you want to return “ducat” or 21 then some other name or 22 then return some other name like this then you have to apply if else condition.

    Copyright 1999- Ducat Creative, All rights reserved.