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.
পৃষ্ঠাসমূহ
Labels
Search Your Article
CS
Pageviews
Showing posts with label Koa.js Tutorial. Show all posts
Showing posts with label Koa.js Tutorial. Show all posts
Thursday, March 2, 2017
Koa.js - Environment
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.
Koa.js - Hello World
So we have set up the development, now it is time to start developing our first app using koa. Create a new file called app.js and type the following in it.
var koa = require('koa'); var app = koa();
Koa.js - Generators
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.
Koa.js - Routing
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:
Koa.js - 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:
Koa.js - 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:
Koa.js - Request Object
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:
Koa.js - Response Object
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:
Koa.js - Redirects
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:
Koa.js - Error Handling
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. For example:
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. For example:
Koa.js - 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.
Koa.js - Templating
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.
Koa.js - 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 koa-body To install this, go to your terminal and use:
Koa.js - File Uploading
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:
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:
Koa.js - 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.
We need a middleware to serve this purpose. Go ahead and install koa-serve:
We need a middleware to serve this purpose. Go ahead and install koa-serve:
$ npm install --save koa-static
Koa.js - 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.
Koa.js - 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. Sessions solve exactly this problem. You assign the client an ID and it makes all further requests using that ID. Information associated with the client is stored on the server linked to this ID.
Koa.js - 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.
Koa.js - Compression
Compression is a simple, effective way to save bandwidth and speed up your site. It is only compatible with modern browsers and should be used with caution if your users use legacy browsers as well.
When sending responses from the server, if compression is used, it can greatly improve the load time. We'll be using a middleware called koa-compress to take care of compression of files as well as setting appropriate headers.
When sending responses from the server, if compression is used, it can greatly improve the load time. We'll be using a middleware called koa-compress to take care of compression of files as well as setting appropriate headers.
Subscribe to:
Posts (Atom)