Koajs, Expressjs and sailsjs framework used with nodejs
Posted By : Sakshi Gadia | 08-Feb-2018
Koa.js
Koa is a framework, its library consist of basic HTTP functionality. Koa uses Es6 generators, promises and async functions so that callback hell is not occurred and it also simplify error handling. Koa middleware flows in a stack-like manner. It is better than express as Koa has no callback hell and also it has better error handling using try catch.
Installation
npm install koa
Koa Context
Koa exposes its own ctx.request and ctx.response objects unlike of nodejs req and res objects. ctx is often used as the parameter name for the context object.
app.use(async (ctx, next) => {
// ctx is the Context
ctx;
// ctx.request is a Koa request
ctx.request;
//ctx.response is a Koa Response
ctx.response;
// Pass the request to the next middleware function
await next();
});
await next() pass the request to the next middleware function in the stack. The incoming message can be accessed as a request property on the Context and server response can be accessed as a response property on the Context.
Create Koa router for route handle.
import Koa from 'koa';
import Router from 'koa-router';
const app = new Koa();
const router = new Router(); //Create a new router.
app.use(async (ctx, next) => {
// Log the request to the console
console.log('Url:', ctx.url);
// Pass the request to the next middleware function
await next();
});
router.get('/*', async (ctx) => {
ctx.body = 'Hello World!';
});
app.use(router.routes());
app.listen(3000);
console.log('Server running on port 3000');
Express.js
Express is one of the popular framework used with node js. Express is a series of middleware function calls that allows to respond HTTP Requests. It allows you to dynamicallly render html pages based on passing agruments to the templates. Express support various HTTP request method (GET, POST, PUT and so on).
import express from "express";
const app = express();
//Creating Router() object
const router = express.Router();
// Router middleware, mentioned it before defining routes.
router.use(function(req,res,next){
next() //next() function will take your router to next routes.
});
// Provide all routes here
router.get("/",function(req,res){
res.json({"message" : "Hello World"});
});
// Tell express to use this router with / before.
app.use("/",router);
// Listen to this Port
app.listen(3000,function(){
console.log("Live at Port 3000");
});
app.use() is used to specify middleware as the callback function.
express.Router() is used to create Router Object.
Sails.js
Sails.js is a framework that provides routing, url mapping to controllers and view like other web application framework did. Sails.js provides a model-view controller (MVC) pattern for implementing data-driven APIs. Sails.js is built on top of Node.js and it makes use of Express.js for handling HTTP requests.
Installation
Install latest sails globally by:
sudo npm install sails -g
As sails comes with blueprints, it can server without writing any code. Create your sails app by:
sails new app
cd app
For running your server you have to use
sails lift
Sails is built on Nodejs, Expressjs and Socket.io.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Sakshi Gadia
An experienced MEAN Stack developer having good knowledge in Nodejs, MongoDb. Apart from these in my spare time, I enjoy playing chess and ready to learn new technologies.