InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What Is Koa.js File Uploading? |
|
Answer» Web applications NEED to provide the functionality to allow file uploads. Let us see how we can receive files from the clients and store them on our server. We have already used the koa-body middleware for parsing requests. This middleware is also used for handling file uploads. Let us create a form that allows us to upload files and then save these files using koa. First create a template called file_upload.pug with the following contents: html Web applications need to provide the functionality to allow file uploads. Let us see how we can receive files from the clients and store them on our server. We have already used the koa-body middleware for parsing requests. This middleware is also used for handling file uploads. Let us create a form that allows us to upload files and then save these files using koa. First create a template called file_upload.pug with the following contents: html |
|
| 2. |
What Is Koa.js Form Data? |
|
Answer» 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 koa-body To install this, go to your terminal and use: $ npm install --save koa-body 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 koa-body To install this, go to your terminal and use: $ npm install --save koa-body |
|
| 3. |
What Is Include And Components? |
|
Answer» Pug provides a very intuitive way to create components for a web page. For example, if you see a news website, the header with logo and categories is always fixed. Instead of copying that to evey view we create we can use an include. Following example shows how we can use an include: Create 3 views with the following code: HEADER.PUG div.header. I'm the header for this website. CONTENT.PUG html FOOTER.PUG div.footer. I'm the footer for this website. Create a route for this as follows: var koa = REQUIRE('koa'); Pug provides a very intuitive way to create components for a web page. For example, if you see a news website, the header with logo and categories is always fixed. Instead of copying that to evey view we create we can use an include. Following example shows how we can use an include: Create 3 views with the following code: HEADER.PUG div.header. I'm the header for this website. CONTENT.PUG html FOOTER.PUG div.footer. I'm the footer for this website. Create a route for this as follows: var koa = require('koa'); |
|
| 4. |
How To Passing Values To Templates? |
|
Answer» When we render a PUG template, we can actually PASS it a value from our route HANDLER, which we can then use in our template. Create a new route handler with the following: var koa = require('koa'); When we render a pug template, we can actually pass it a value from our route handler, which we can then use in our template. Create a new route handler with the following: var koa = require('koa'); |
|
| 5. |
What Is Attributes? |
|
Answer» To DEFINE attributes, we use a comma seperated list of attributes, in PARANTHESIS. Class and ID attributes have special representations. The following line of code covers defining attributes, classes and id for a given HTML tag. div.container.COLUMN.main#DIVISION(width="100",height="100") This line of code, get converted to: <div class="container column main" id="division" width="100" height="100"></div> To define attributes, we use a comma seperated list of attributes, in paranthesis. Class and ID attributes have special representations. The following line of code covers defining attributes, classes and id for a given html tag. div.container.column.main#division(width="100",height="100") This line of code, get converted to: <div class="container column main" id="division" width="100" height="100"></div> |
|
| 6. |
What Is Koa.js Templating? |
|
Answer» Pug is a templating engine. 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. To USE Pug with koa, we need to install it, $ npm install --save pug koa-pug Now create a new directory called views. Inside that create a file called first_view.pug, and enter the following data in it. doctype html Pug is a templating engine. 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. To use Pug with koa, we need to install it, $ npm install --save pug koa-pug Now create a new directory called views. Inside that create a file called first_view.pug, and enter the following data in it. doctype html |
|
| 7. |
What Is Third Party Middleware? |
|
Answer» A list of Third party middleware for EXPRESS is available here. FOLLOWING are some of the most COMMONLY USED middlewares:
A list of Third party middleware for express is available here. Following are some of the most commonly used middlewares: |
|
| 8. |
What Is Order Of Middleware Calls? |
|
Answer» One of the most important things about middleware in KOA is that the order in which they are written/included in your file, are the order in which they are executed DOWNSTREAM. As SOON as we hit a yield statement in a middleware, it switches to the next middleware in line till we reach the last. Then again we start moving BACK up and resuming functions from yield statements. For example, in the following code snippet, the first FUNCTION executes first till yield, then the second middleware till yield, then the third. As we have no more middlewares here, we start moving back up, executing in reverse order, ie, third, second, first. One of the most important things about middleware in koa is that the order in which they are written/included in your file, are the order in which they are executed downstream. As soon as we hit a yield statement in a middleware, it switches to the next middleware in line till we reach the last. Then again we start moving back up and resuming functions from yield statements. For example, in the following code snippet, the first function executes first till yield, then the second middleware till yield, then the third. As we have no more middlewares here, we start moving back up, executing in reverse order, ie, third, second, first. |
|
| 9. |
What Is Cascading In Koa.js? |
|
Answer» Middleware functions are functions that have access to the context object and the NEXT middleware function in the APPLICATION’s REQUEST-response cycle. These functions are used to modify request and response objects for tasks like parsing request bodies, adding response HEADERS, etc. Koa goes a step further by yielding 'downstream', then flowing control back 'upstream'. This EFFECT is called cascading. Middleware functions are functions that have access to the context object and the next middleware function in the application’s request-response cycle. These functions are used to modify request and response objects for tasks like parsing request bodies, adding response headers, etc. Koa goes a step further by yielding 'downstream', then flowing control back 'upstream'. This effect is called cascading. |
|
| 10. |
What Is Koa.js Error Handling? |
|
Answer» ERROR handling plays an important PART in building WEB APPLICATIONS. Koa uses middlewares for this purpose as well. In koa you add a middleware that does try { yield next } as one of the first middleware. If we encounter any error downstream, we return to the associated CATCH clause and handle the error here. Error handling plays an important part in building web applications. Koa uses middlewares for this purpose as well. In koa you add a middleware that does try { yield next } as one of the first middleware. If we encounter any error downstream, we return to the associated catch clause and handle the error here. |
|
| 11. |
What Is Koa.js Redirects? |
|
Answer» Redirection is very important when creating websites. If a malformed URL is requested or there are some errors on your server, you should redirect them to the respective ERROR pages. Redirects can also be used to keep people out of restricted areas of your website. Let us create an error page and redirect to that page whenever someone requests a malformed URL: VAR koa = require('koa'); When we run this code and navigate to any route other than /hello, we'll be REDIRECTED to /not_found. We have placed the middleware at the end(app.use function CALL to this middleware). This ensures we reach the middleware at last and send the corresponding response. Redirection is very important when creating websites. If a malformed URL is requested or there are some errors on your server, you should redirect them to the respective error pages. Redirects can also be used to keep people out of restricted areas of your website. Let us create an error page and redirect to that page whenever someone requests a malformed URL: var koa = require('koa'); When we run this code and navigate to any route other than /hello, we'll be redirected to /not_found. We have placed the middleware at the end(app.use function call to this middleware). This ensures we reach the middleware at last and send the corresponding response. |
|
| 12. |
What Is Koa.js Response Object? |
|
Answer» A Koa Response object is an abstraction on top of node's vanilla response object, providing additional functionality that is useful for every day HTTP server development. The Koa response object is embeded in the context object, this. Lets log out the response object WHENEVER we GET a REQUEST: var koa = require('koa'); A Koa Response object is an abstraction on top of node's vanilla response object, providing additional functionality that is useful for every day HTTP server development. The Koa response object is embeded in the context object, this. Lets log out the response object whenever we get a request: var koa = require('koa'); |
|
| 13. |
What Is Koa.js Request Object? |
|
Answer» A Koa REQUEST object is an abstraction on top of NODE's vanilla request object, providing additional functionality that is useful for every day HTTP server development. The Koa request object is embeded in the context object, this. Lets log out the request object whenever we get a request: VAR koa = require('koa'); A Koa Request object is an abstraction on top of node's vanilla request object, providing additional functionality that is useful for every day HTTP server development. The Koa request object is embeded in the context object, this. Lets log out the request object whenever we get a request: var koa = require('koa'); |
|
| 14. |
What Is Koa.js Http Methods? |
|
Answer» 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:
These are the most common 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: These are the most common HTTP methods. |
|
| 15. |
What Is Pattern Matched Routes? |
|
Answer» You can also use REGEX to restrict URL parameter matching. LET's say you need the id to be 5 digits long number. You can use the following ROUTE definition: var koa = require('koa'); Note that this will only match the requests that have a 5 digit long id. You can use more complex regexes to match/validate your routes. If NONE of your routes match the request, you'll get a Not found message as response. You can also use regex to restrict URL parameter matching. Let's say you need the id to be 5 digits long number. You can use the following route definition: var koa = require('koa'); Note that this will only match the requests that have a 5 digit long id. You can use more complex regexes to match/validate your routes. If none of your routes match the request, you'll get a Not found message as response. |
|
| 16. |
What Is Koa.js Url Building? |
|
Answer» 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: var koa = require('koa'); 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: var koa = require('koa'); |
|
| 17. |
What Is Koa.js Routing? |
|
Answer» Web frameworks PROVIDE resources such as HTML pages, scripts, images, etc. at different routes. Koa does not support routes in the core module. We need to use the koa-router module to easily create routes in koa. Install this module using: NPM install --save koa-router Now that we have koa-router installed, lets look at a simple GET route example: VAR koa = require('koa'); If we run our application and go to localhost:3000/hello, the server receives a get request at route "/hello", our koa app executes the callback function attached to this route and sends "Hello World!" as the RESPONSE. Web frameworks provide resources such as HTML pages, scripts, images, etc. at different routes. Koa does not support routes in the core module. We need to use the koa-router module to easily create routes in koa. Install this module using: npm install --save koa-router Now that we have koa-router installed, lets look at a simple GET route example: var koa = require('koa'); If we run our application and go to localhost:3000/hello, the server receives a get request at route "/hello", our koa app executes the callback function attached to this route and sends "Hello World!" as the response. |
|
| 18. |
What Is Koa.js Generators? |
|
Answer» One of the most exciting new features coming in JavaScript ES6 is a new breed of function, called a generator. Before generators, the whole script used to usually execute in a top to bottom order, without an easy way to stop code execution and resuming with the same stack LATER. Generators are functions which can be EXITED and later re-entered. Their context (variable BINDINGS) will be saved ACROSS re-entrances. Generators allow us to stop code execution in between. So LETS have a look at a simple generator: var generator_func = function* (){ One of the most exciting new features coming in JavaScript ES6 is a new breed of function, called a generator. Before generators, the whole script used to usually execute in a top to bottom order, without an easy way to stop code execution and resuming with the same stack later. Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances. Generators allow us to stop code execution in between. So lets have a look at a simple generator: var generator_func = function* (){ |
|
| 19. |
How To Use Npm? |
|
Answer» There are 2 ways to install a package using NPM: globally and locally.
Whenever we create a project using npm, we need to provide a package.json file, which has all the DETAILS about our project. npm make it easy for us to set up this file. Let us set up our development project.
There are 2 ways to install a package using npm: globally and locally. Whenever we create a project using npm, we need to provide a package.json file, which has all the details about our project. npm make it easy for us to set up this file. Let us set up our development project. |
|
| 20. |
What Is Node Package Manager(npm)? |
|
Answer» npm is the package MANAGER for NODE. The npm Registry is a public collection of packages of open-source code for Node.js, front-end WEB APPS, mobile apps, robots, routers, and countless other needs of the JavaScript community. npm allows us to access all these packages and install them locally. You can browse through the list of packages available on npm at npmJS. npm is the package manager for node. The npm Registry is a public collection of packages of open-source code for Node.js, front-end web apps, mobile apps, robots, routers, and countless other needs of the JavaScript community. npm allows us to access all these packages and install them locally. You can browse through the list of packages available on npm at npmJS. |
|
| 21. |
Explain Koa.js Environment? |
|
Answer» To get started with developing using the Koa 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 You should get an output similar to: v5.0.0 Please ensure your node version is above 6.5.0. Now that we have Node and npm set up, LET us understand what npm is and how to USE it. To get started with developing using the Koa 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 You should get an output similar to: v5.0.0 Please ensure your node version is above 6.5.0. Now that we have Node and npm set up, let us understand what npm is and how to use it. |
|
| 22. |
What Is Mongodb And Mongoose? |
|
Answer» MONGODB is an open-source, DOCUMENT database designed for ease of DEVELOPMENT and scaling. We'll use this database to STORE data. Mongoose is a client API for node.js which makes it easy to access our database from our koa application. MongoDB is an open-source, document database designed for ease of development and scaling. We'll use this database to store data. Mongoose is a client API for node.js which makes it easy to access our database from our koa application. |
|
| 23. |
What Is Pug? |
|
Answer» Pug(EARLIER known as Jade) is a terse language for writing HTML templates. It:
Pug(earlier known as Jade) is a terse language for writing HTML templates. It: |
|
| 24. |
Why Koa? |
|
Answer» KOA has a small footprint(600 LoC) and is a very thin layer of abstraction over node to create server side APPS. It is fully pluggable and has a HUGE COMMUNITY. This also allows us to easily extend koa and use it according to our needs. It is built using the bleeging EDGE technology(ES6) which gives it an edge over older frameworks like express. Koa has a small footprint(600 LoC) and is a very thin layer of abstraction over node to create server side apps. It is fully pluggable and has a huge community. This also allows us to easily extend koa and use it according to our needs. It is built using the bleeging edge technology(ES6) which gives it an edge over older frameworks like express. |
|
| 25. |
What Is Koa? |
|
Answer» KOA provides a minimal interface to us to build our applications. It is a very small framework(600 LoC) which provides us the required tools to build our app and is quite flexible, there are numerous modules available on npm for Koa, which can be directly plugged into it. Koa can be thought of as the core of express.js without all the BELLS and WHISTLES. Koa provides a minimal interface to us to build our applications. It is a very small framework(600 LoC) which provides us the required tools to build our app and is quite flexible, there are numerous modules available on npm for Koa, which can be directly plugged into it. Koa can be thought of as the core of express.js without all the bells and whistles. |
|