TpointTech Express Flashcards

1
Q

What is Express js?

A

Express is a fast, assertive, essential and moderate web framework of Node.js. You can assume express as a layer built on the top of the Node.js that helps manage a server and routes. It provides a robust set of features to develop web and mobile applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the features of Express js?

A

Let’s see some of the core features of Express framework:

It can be used to design single-page, multi-page and hybrid web applications.
It allows to setup middlewares to respond to HTTP Requests.
It defines a routing table which is used to perform different actions based on HTTP method and URL.
It allows to dynamically render HTML Pages based on passing arguments to templates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why do we use Express?

A

Ultra fast I/O
Asynchronous and single threaded
MVC like structure
Robust API makes routing easy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does express look like?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to install Express js?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the most important modules to install while installing express and why are they important?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the express Request Object?

A

Express.js Request and Response objects are the parameters of the callback function which is used in Express applications.

The express.js request object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.

Express.js Request Object Properties
The following table specifies some of the properties associated with request object.

Index
Properties
Description
1.
req.app
This is used to hold a reference to the instance of the express application that is using the middleware.
2.
req.baseurl
It specifies the URL path on which a router instance was mounted.
3.
req.body
It contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser.
4.
req.cookies
When we use cookie-parser middleware, this property is an object that contains cookies sent by the request.
5.
req.fresh
It specifies that the request is “fresh.” it is the opposite of req.stale.
6.
req.hostname
It contains the hostname from the “host” http header.
7.
req.ip
It specifies the remote IP address of the request.
8.
req.ips
When the trust proxy setting is true, this property contains an array of IP addresses specified in the ?x-forwarded-for? request header.
9.
req.originalurl
This property is much like req.url; however, it retains the original request URL, allowing you to rewrite req.url freely for internal routing purposes.
10.
req.params
An object containing properties mapped to the named route ?parameters?. For example, if you have the route /user/:name, then the “name” property is available as req.params.name. This object defaults to {}.
11.
req.path
It contains the path part of the request URL.
12.
req.protocol
The request protocol string, “http” or “https” when requested with TLS.
13.
req.query
An object containing a property for each query string parameter in the route.
14.
req.route
The currently-matched route, a string.
15.
req.secure
A Boolean that is true if a TLS connection is established.
16.
req.signedcookies
When using cookie-parser middleware, this property contains signed cookies sent by the request, unsigned and ready for use.
17.
req.stale
It indicates whether the request is “stale,” and is the opposite of req.fresh.
18.
req.subdomains
It represents an array of subdomains in the domain name of the request.
19.
req.xhr
A Boolean value that is true if the request’s “x-requested-with” header field is “xmlhttprequest”, indicating that the request was issued by a client library such as jQuery

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Other used request object methods?

A

req.accepts (types)
This method is used to check whether the specified content types are acceptable, based on the request’s Accept HTTP header field.

req.get(field)
This method returns the specified HTTP request header field.

req.is(type)
This method returns true if the incoming request’s “Content-Type” HTTP header field matches the MIME type specified by the type parameter.

req.param(name [, defaultValue])
This method is used to fetch the value of param name when present.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Express.s Response Object
What is a response object?

A

The Response object (res) specifies the HTTP response which is sent by an Express app when it gets an HTTP request.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the response object do?

A

What it does

It sends response back to the client browser.
It facilitates you to put new cookies value and that will write to the client browser (under cross domain rule).

Once you res.send() or res.redirect() or res.render(), you cannot do it again, otherwise, there will be uncaught error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are some of the common response obejct properties?

A

res.download(path [, filename] [, fn])
This method transfers the file at path as an “attachment” and enforces the browser to prompt user for download.

res.end([data] [, encoding])
This method is used to end the response process.

res.format(object)
This method performs content negotiation on the Accept HTTP header on the request object, when present.

res.get(field)
This method provides HTTP response header specified by field.

res.json([body])
This method returns the response in JSON format.

res.jsonp([body])
This method returns response in JSON format with JSONP support.

res.links(links)
This method populates the response?s Link HTTP header field by joining the links provided as properties of the parameter.

res.location(path)
This method is used to set the response location HTTP header field based on the specified path parameter.

res.render(view [, locals] [, callback])
This method renders a view and sends the rendered HTML string to the client.

res.send([body])
This method is used to send HTTP response.

res.sendFile(path [, options] [, fn])
This method is used to transfer the file at the given path. It sets the Content-Type response HTTP header field based on the filename’s extension.

res.set(field [, value])
This method is used to set the response of HTTP header field to value.

res.status(code)
This method sets an HTTP status for the response.

res.type(type)
This method sets the content-type HTTP header to the MIME type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are GET requests?

A

GET and POST both are two common HTTP requests used for building REST API’s. GET requests are used to send only limited amount of data because data is sent into header while POST requests are used to send large amount of data because data is sent in the body.

Express.js facilitates you to handle GET and POST requests using the instance of express.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is express POST Request?

A

GET and POST both are two common HTTP requests used for building REST API’s. POST requests are used to send large amount of data.

Express.js facilitates you to handle GET and POST requests using the instance of express.

Express.js POST Method
Post method facilitates you to send large amount of data because data is send in the body. Post method is secure because data is not visible in URL bar but it is not used as popularly as GET method. On the other hand GET method is more efficient and used more than POST.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Define routing in express.js

A

Routing is made from the word route. It is used to determine the specific behavior of an application. It specifies how an application responds to a client request to a particular route, URI or path and a specific HTTP request method (GET, POST, etc.). It can handle different types of HTTP requests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are cookies?

A

Cookies are small piece of information i.e. sent from a website and stored in user’s web browser when user browses that website. Every time the user loads that website back, the browser sends that stored data back to website or server, to recognize user.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Defining a route in express.js?

A

Cookie-parser parses Cookie header and populate req.cookies with an object keyed by the cookie names.

Let’s define a new route in your express app like set a new cookie:

16
Q

What is file upload in express js?

A

File: package.json

{
“name”: “file_upload”,
“version”: “0.0.1”,
“dependencies”: {
“express”: “4.13.3”,
“multer”: “1.1.0”
},
“devDependencies”: {
“should”: “~7.1.0”,
“mocha”: “~2.3.3”,
“supertest”: “~1.1.0”
}
}

17
Q

Explain a middleware?

A

Express.js Middleware are different types of functions that are invoked by the Express.js routing layer before the final request handler. As the name specified, Middleware appears in the middle between an initial request and final intended route. In stack, middleware functions are always invoked in the order in which they are added.

Middleware is commonly used to perform tasks like body parsing for URL-encoded or JSON requests, cookie parsing for basic cookie handling, or even building JavaScript modules on the fly.

18
Q

What is a middleware function?

A

Middleware functions are the functions that access to the request and response object (req, res) in request-response cycle.

A middleware function can perform the following tasks:

It can execute any code.
It can make changes to the request and the response objects.
It can end the request-response cycle.
It can call the next middleware function in the stack.

19
Q

What are the possibly used middleware in Express.js app?

A

Application-level middleware
Router-level middleware
Error-handling middleware
Built-in middleware
Third-party middleware

20
Q

How do middlewares work?

A
21
Q

What is scaffolding?

A

Scaffolding is a technique that is supported by some MVC frameworks.

It is mainly supported by the following frameworks:

Ruby on Rails,OutSystems Platform, Express Framework, Play framework, Django, MonoRail, Brail, Symfony, Laravel, CodeIgniter, Yii, CakePHP, Phalcon PHP, Model-Glue, PRADO, Grails, Catalyst, Seam Framework, Spring Roo, ASP.NET etc.

Scaffolding facilitates the programmers to specify how the application data may be used. This specification is used by the frameworks with predefined code templates, to generate the final code that the application can use for CRUD operations (create, read, update and delete database entries).

22
Q

Express scaffold?

A
23
Q

What is a Template Engine?

A

What is a template engine
A template engine facilitates you to use static template files in your applications. At runtime, it replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. So this approach is preferred to design HTML pages easily.

Following is a list of some popular template engines that work with Express.js:

Pug (formerly known as jade)
mustache
dust
atpl
eco
ect
ejs
haml
haml-coffee
handlebars

24
Q

How do you use template engines?

A