পৃষ্ঠাসমূহ

Search Your Article

CS

Pageviews

Showing posts with label ExpressJS Tutorial. Show all posts
Showing posts with label ExpressJS Tutorial. Show all posts

Friday, February 17, 2017

ExpressJS - Overview

A web application framework provides you with a simple API to build websites, webapps and backends. You need not worry about low level protocols, processes, etc.

ExpressJS - Environment

To get started with developing using the Express framework, you need to have Node and npm(node package manager) installed. If you don’t already have these, head over to Node setup to install node on your local system. Confirm that node and npm are installed by running the following commands in your terminal.
node --version

ExpressJS - Hello World

So we have set up the development, now it is time to start developing our first app using express. Create a new file called index.js and type the following in it.
var express = require('express');
var app = express();

ExpressJS - Routing

Web frameworks provide resources such as HTML pages, scripts, images, etc. at different routes. The following function signature is used to define routes in an express application:
app.METHOD(PATH, HANDLER)

ExpressJS - HTTP Methods

The HTTP method is supplied in the request and specifies the operation that the client has requested. The below table summarizes the most used HTTP methods:

ExpressJS - URL Building

We can now define routes, but those are static or fixed. To use dynamic routes, we need to provide different types of routes. Using dynamic routes allows us to pass parameters and process based on them. Here is an example of a dynamic route:

ExpressJS - Middleware

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. These functions are used to modify req and res objects for tasks like parsing request bodies, adding response headers, etc.
Here is a simple example of a middleware function in action:

ExpressJS - Templating

Pug is a templating engine for express. Templating engines are used to remove the cluttering of our server code with HTML, concatenating strings wildly to existing HTML templates. Pug is a very powerful templating engine which has a variety of features including filters, includes, inheritance, interpolation, etc. There is a lot of ground to cover on this.

ExpressJS - Serving static files

Static files are files that clients download as they are from the server. Create a new directory, public. Express, by default doesn't allow you to serve static files. You need to enable it using the following built-in middleware.

ExpressJS - Form data

Forms are an intergral part of the web. Almost every website we visit offers us forms that submit or fetch some information for us. To get started with forms, we will first install the body-parser(for parsing JSON and url-encoded data) and multer(for parsing multipart/form data) middleware. To install these, go to your terminal and use:

ExpressJS - Database

We are receiving the requests, but are not storing them anywhere. We need a Database to store the data. We'll use a famous NoSQL database called MongoDB. To install and read about Mongo, head over to this link.

ExpressJS - Cookies

Cookies are simple, small files/data that are sent to client with a server request and stored on the client side. Every time the user loads the website back, this cookie is sent with the request. This helps us keep track of the users actions. There are numerous uses of HTTP Cookies.

ExpressJS - Sessions

Because HTTP is stateless, in order to associate a request to any other request, you need a way to store user data between HTTP requests. Cookies and URL parameters are both suitable ways to transport data between client and server. But they are both readable and on the client side.

ExpressJS - Authentication

Authentication is a process in which the credentials provided are compared to those on file in a database of authorized users' information on a local operating system or within an authentication server. If the credentials match, the process is completed and the user is granted authorization for access.

ExpressJS - RESTFul APIs

To create mobile applications, single page applications, use AJAX calls and provide data to clients, you'll need an API. An popular architectural style of how to structure and name these APIs and the endpoints is called REST(Representational Transfer State). HTTP 1.1 was designed keeping REST principles in mind. REST was introduced by Roy Fielding in 2000 in his paper Fielding Dissertions.

ExpressJS - Scaffolding

Scaffolding allows us to easily create a skeleton for a web application. We manually created our public directory, added middleware, created separate route files, etc. A scaffolding tool sets up all these things for us so that we can directly get started with building our application.

ExpressJS - Error Handling

Error handling in Express is done using middleware. But this middleware has special properties. The error handling middleware are Defined in the same way as other middleware functions, except error-handling functions MUST have four arguments instead of three: (err, req, res, next). For example, to send a response on any error, we can use:

ExpressJS - Debugging

Express uses the Debug module to internally log information about route matching, middleware functions, application mode, etc.
To see all internal logs used in express, set the DEBUG environment variable to express:* when starting the app:
DEBUG=express:* node index.js
You'll get a colorized output like:

ExpressJS - Best Practices

Unlike Django and rails which have a defined way of doing things, file structure, etc, Express is unopinionated on this. What this means is you can structure the application however you like. But as your application grows in size, its very difficult to maintain it if it doesn't have a well defined structure.

ExpressJS - Resources

Here is a list of resources I used while learning Express as well as while preparing this tutorial.
  1. The most important link is of course to the express API docs: http://expressjs.com/en/4x/api.html
  2. The guides provided on the express website are also quite helpful: