Most Important Interview Questions and answers

Interviews are the most important and final stage of getting any job. In the blog of Interview questions, you will know which are the most common questions asked in interviews. Are you also looking…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Express Web Framework

Express is a popular unopinionated web framework, written in JavaScript and hosted within the Node.js runtime environment. It is the most popular Node web framework, and is the underlying library for a number of other popular Node web frameworks. It can -

While express itself is pretty small but some developers have created compatible middleware packages to address almost any web development problem. There are a lot of libraries with which you can work like cookies, sessions, user logins, URL parameters, post data, security headers, and many more.

Express Code:

When a request is received the application works out what action is needed based on the URL pattern and possibly associated information contained in post data or get data. Depending on what is required it may then read or write information from a database or perform other tasks required to satisfy the request. The application will then return a response to the web browser, often dynamically creating an HTML page for the browser to display by inserting the retrieved data into placeholders in an HTML template.

Express provides methods to specify what function is called for a particular HTTP verb (get, post, set, etc.) and URL pattern ("Route"), and methods to specify what template ("view") engine is used, where template files are located, and what template to use to render a response. You can use Express middleware to add support for cookies, sessions, and users, getting post/get parameters, etc. You can use any database mechanism supported by Node (Express does not define any database-related behaviour).

In middle part of the code the three written lines are route definition. The middle part of the code (the three lines starting with app.get) shows a route definition. The app.get() method specifies a callback function that will be invoked whenever there is an HTTP get request with a path (“/”) relative to the site root. The callback function takes a request and a response object as arguments, and simply calls send on the response to return the string "Hello World!".

In last three lines we create a server which runs on port 3000 and log the message to console to ensure that the server is created successfully although the message written can be of your own choice. To see if your server is running go to localhost:3000 in the browser to see the return of example response.

Importing and creating module:

Module is a JavaScript library that can be imported into other code using the function require(). Express itself is a module, as are the middleware and database libraries that we use in our Express applications. In the above example we saw how we imported the express module by using it’s name. Name of the module to be required must always be written inside string. After importing the module by using require() it returns us an object and we call the object to create an Express application. After calling it we can access the properties and functions of the application object. You can also create your own modules and export/import it.

In order to access objects outside of a module we just need to expose them as additional properties on the exports object.

As seen in the above example the code the is being exported so that it can be used by other files. For exporting the module you first need to store it inside a file so that it can be exported.

What are routes?

Routes are those which allow you to match particular patterns of characters in a URL, and extract some values from the URL and pass them as parameters to the route handler (as attributes of the request object passed as a parameter). Often it is useful to group route handlers for a particular part of a site together and access them using a common route-prefix. In Express this is achieved by using the express.Router object.

For example:

To use the router in our main app file we would then require() the route module (name of the file), then call use() on the Express application to add the Router to the middleware handling path. The two routes will then be accessible from /filename/ and /filename/about/.

Middlewares:

Middleware is used extensively in Express apps, for tasks from serving static files to error handling, to compressing HTTP responses. Whereas route functions end the HTTP request-response cycle by returning some response to the HTTP client, middleware functions typically perform some operation on the request or response and then call the next function in the “stack”, which might be more middleware or a route handler. The order in which middleware is called is up to the app developer. To use third party middleware you first need to install it into your app using NPM.

Call the use() on the express application object to create a middleware.

The only difference between a middleware function and a route handler callback is that middleware functions have a third argument next, which middleware functions are expected to call if they are not that which completes the request cycle (when the middleware function is called, this contains the next function that must be called).

Add a comment

Related posts:

How the USA is Officially Shorting Small Businesses with the PPP Loan

Promise of PPP loan to keep employment and pay back nothing is a pipe dream with a underlying debt crisis in the first place It is fools gold and the administration and policies makers took a bad…

InsurTech Ohio Interview with Chris Olsen of Drive Capital

Chris Olsen is a Co-Founder and Partner at Drive Capital, a venture capital firm based in Columbus, Ohio, focused on investing in Midwest companies that have the conviction and experience to solve…

Everyday Writing Will Never Work

These strategies provide an excuse for failure. They give a false sense of accomplishment. Write more instead of writing better — it’s the motto. This approach gets rid of the author’s…