i
Creating a Web Server
Handling Get Requests
Handling Post Requests
Event Handling
Event Loop
Routing
Streams
Debugging a Node application
Framework in Node.js
Express.js
Templating Engine
Invoking a REST Service
Pre-requisites for Node.js
Introduction to Node.js
What to use Node.js for?
Node.js - Environment Setup
REPL Terminal
Express.js is among most Popular frameworks creating a web application in Node.js. Using Express we can handle requests, create views and manage routes.
Express simplifies the application development for Node.js developers by introducing a set of APIs which extend Node.js and are easy to use.
Advantages of using Express.js
Writing request handlers for different URLs in a straight forward manner.
We can use templating engines for view generation.
Using the Middleware concept, we can remove cross-cutting concerns like Logging, Authentication etc.
To develop an Express application, a development environment with below packages will be needed
1. Node.js
you can check it using
node –v
2. Express package installation
Using
npm I express
In order to implement an express application:
Steps to Follow
1. Setup Express development environment
2. Create a basic server using Express
3. Implement Routing functionality(optional)
4. Generate Views according to templating engine (optional)
5. Connect to the database (if data CRUD operation are to be performed)
6. Use Middleware in the application (optional; if data is to be )
7. Manage session and security features in the application.
Before making an express application, we need to install the Express module from npm library.
Using
>Npm I express
Make sure you have node and npm already installed on your device before installing Express..
Lets look an example for Express Demo
Express-demo.js
From the above code snippet, kets discuss a few concepts
Routing
To explore various features of a web application, we would need to request different URLs for a particular feature. For a particular web application, all the URLs will reach to the same server, but paths will be different. It is the server's responsibility to route the URLs to the appropriate features depending on the path.
In the above code snippet, we have a server created at https://localhost:3000/
but to access different functionalities, we would need different routes
https://localhost:3000/books for returning books collection stored.
https://localhost:3000/ for taking the user to home page URL.
Home.html
Output
‘’localhost:3000/
On checking MongoDB database
‘localhost:3000/books’
How can we send data from Client to Server
We can send the data in the http server in multiple ways using Express Framework.
Inside Request body
Inside Header
Inside Query Parameters
Send it inside resource parameters
Don't miss out!