REST services and KoaJS
RESTful services
- REST stands for REpresentational State Transfer protocol.
- Resources-based - Things are built around resources or services.
- URI are resources identified.
- The same resource may be referred to by multiple URIs.
- To pass data used XML or JSON.
- Scalable, maintainable, and also lightweight.
HTTP Verbs
- GET - to get resource
- POST - to input new resource
- PUT - to replace an included resource
- DELETE - to remove a resource
- OPTIONS - to get allowed for all options
- HEAD - to get only the response header
Koa - Next generation web framework for Node.js
Introduction
Koa is a new web framework design and developed by the same development team that, designed the Express. The aim of developing this framework is to develop a framework that is smaller and as well as more expressive and also for the robust foundation for web application and web API. By influencing to increase async functions, Koa allows trench callbacks and slowly increases error-handling as well. Koa doesn't allow to make a packet in any middleware within its core, and it gives a perfect suite for methods that make writing servers fast and reliable.
Installation
When installing koa, it requires node v7.6.0 or higher for ES2015 and also needs async function support. Following steps needed for quick installation, with your required version manager.
$ nvm install 7
$ npm i koa
$ node my-koa-app.js
Application
The Koa applications are composed and executed in a stack-like manner upon request when an object containing use as an array of middleware functions. Koa is also a middleware system that is similar to many other middleware systems, that may you have already used such as Ruby's Rack, connect, and etc.
Anyway, the purpose of having a key design decision is to supply a high-level 'sugar' or else low-level middleware layer. This is helping to improve robustness, interoperability and making writing middleware much more reliable.
This framework includes methods for common tasks such as cache freshness, negotiation, redirection among others, and proxy support. Koa maintains a small footprint, without having a large number of useful methods and there is no middleware are packed.
Let us print Hello World using Koa
Important Hello world application. Run the below code and see your first application with Koa.
const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
Comments
Post a Comment