First ExpressJs App

In this article, we are going to know about how to install and run the first ExpressJs App.

What is ExpressJs?

So before going to know about ExpressJs. We have to know about nodejs.

NodeJS is an open-source and cross-platform runtime environment for executing JavaScript code outside of a browser. You need to know that NodeJS isn’t a framework, and it’s not a programing language. It provides an event-driven, non-blocking (asynchronous) I/O and cross-platform runtime environment for building highly scalable server-side applications using JavaScript. We often use Node.js for building back-end services like APIs, Web App, or Mobile App. It’s utilized in production by large companies like Paypal, Uber, Netflix, Walmart, etc.

Express.js is the most popular Node js web application server framework, which is specifically designed for building single-page, multi-page, and hybrid web applications. It has become the standard server framework for nodejs.

ExpressJs Features

ExpressJs provides different features :

  • Write handlers for requests with different HTTP verbs at different URL paths (routes).

  • Integrate with "view" rendering engines to generate responses by inserting data into templates.

  • Set common web application settings like the port to use for connecting, and the location of templates that are used for rendering the response.

  • Add additional request processing "middleware" at any point within the request handling pipeline.

Install and Run ExpressJs App

For running expressjs in your system you need nodejs. If you don't have nodejs you need to install nodejs. Also, you need npm.

  1. Create a directory and go within that directory

     $ mkdir expressApp
     $ cd expressApp
    
  2. Initialize your project with npm

     $ npm init
    

    This command prompts you for several things, such as the name and version of your application. For now, you can simply hit Enter to accept the defaults for most of them, with the following exception:

     entry point: (index.js)
    

    Enter app.js, or whatever you want the name of the main file to be. If you want it to be index.js, hit Enter to accept the suggested default file name.

  3. Now install Express in the expressApp directory and save it in the dependencies list

     $ npm install express
    
    1. Create a file named index.js and start writing code in your favorite code editor

       var express = require('express');
       var app = express();
       app.get('/', function (req, res) {
         res.send('Hello World!');
       });
       app.listen(3000, function () {
         console.log('Example app listening on port 3000!');
       });
      

      The first line declares a variable that will contain the module called to express, grabbing it from the node_modules folder. The module is a function.

      Assigning the function call to another variable gives you access to a predefined set of tools which will a great deal make your life much easier.

      You could view the variable app as an object, whose methods you are utilizing to build the actual program.

      The listen method starts a server and listens on port 3000 for connections.
      It responds with “Hello World!” to get requests to the root URL (/). For every other path, it will respond with a 404 Not Found.

    2. Run the app using the below command

       $ node index.js
      

Output :

So finally you have successfully run the first express app. You can see the output of the express app in the above image

So I hope you learn something from this article. It is just beginning you can explore express and node js more. That is for today. Thank you so much.