In this article you’ll learn about how to install express js framework in your local machine and how to write a server using express js framework.
What is Express JS
Express JS is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. With a myriad of HTTP utility methods and middleware at your disposal, creating a robust API is quick and easy using Express JS. Express provides a thin layer of fundamental web application features, without obscuring Node.js features that you know and love.
Several popular Node.js frameworks are built on Express:
- Feathers: Build prototypes in minutes and production ready real-time apps in days.
- ItemsAPI: Search backend for web and mobile applications built on Express and Elasticsearch.
- KeystoneJS: Website and API Application Framework / CMS with an auto-generated React.js Admin UI.
- Poet: Lightweight Markdown Blog Engine with instant pagination, tag and category views.
- Kraken: Secure and scalable layer that extends Express by providing structure and convention.
- LoopBack: Highly-extensible, open-source Node.js framework for quickly creating dynamic end-to-end REST APIs.
- Sails: MVC framework for Node.js for building practical, production-ready apps.
- Hydra-Express: Hydra-Express is a light-weight library which facilitates building Node.js Microservices using ExpressJS.
- Blueprint: a SOLID framework for building APIs and backend services
- Locomotive: Powerful MVC web framework for Node.js from the maker of Passport.js
- graphql-yoga: Fully-featured, yet simple and lightweight GraphQL server
- Express Gateway: Fully-featured and extensible API Gateway using Express as foundation
- Dinoloop: Rest API Application Framework powered by typescript with dependency injection
- Kites: Template-based Web Application Framework
- FoalTS: Elegant and all-inclusive Node.Js web framework based on TypeScript.
- NestJs: A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications on top of TypeScript & JavaScript (ES6, ES7, ES8)
- Expressive Tea: A Small framework for building modulable, clean, fast and descriptive server-side applications with Typescript and Express out of the box.
Install Express JS
Now as you have idea about express then let’s start installing express JS framework, but before that you need to install Nodejs first if you already have Nodejs installed then you can continue, if not then please install Nodejs first.
create a directory to hold your application, and make that your working directory.
$ mkdir expressApp
$ cd expressApp
Now use the npm init command to create a package.json file for your application. Checkout to learn more about package.json
$ npm init
Above command prompts you for a number of inputs, such as the name, git repository, license and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them like mentioned below:
Enter app.js, or whatever you want the name of the main file to be. If you want it to be index.js, hit RETURN to accept the suggested default file name as shown in above image.
Now install Express JS in the expressApp directory and save it in the dependencies list. For example:
$ npm install express
To install Express temporarily and not add it to the dependencies list:
$ npm install express –no-save
Express JS is installed in our system now let’s write code to test the framework working or not:
Create Express JS server
// express js server code const express = require('express') const app = express() const port = 3000 app.get('/ping', (req, res) => { res.send('Pong') }) app.get('/', (req, res) => { res.send('Express Server Running') }) app.listen(port, () => { console.log(`Example app listening on port http://localhost:${port}/`) })
Above code starts a app server and listens on port 3000 for connections. The app have two routes first root URL (/
) and second is ping URL (/ping), If you hit root URL (/
) you’ll get “Express Server Running” response or route and for ping URL (/ping) you’ll get “pong” response. For every other path, it will respond with a 404 Not Found.
Run the app with the following command now:
$ node index.js
Then, load http://localhost:3000/ in a browser to see the output.
Root URL (/)
Ping URL (/ping)
404 Not Found
your comments are appreciated and if you wants to see your articles on this platform then please shoot a mail at this address kusingh@programmingeeksclub.com
Thanks for reading