Home >Web Front-end >JS Tutorial >Detailed explanation of IWinter, a routing-to-controller Nodejs library

Detailed explanation of IWinter, a routing-to-controller Nodejs library

小云云
小云云Original
2018-01-18 17:04:221447browse

This article mainly introduces the detailed explanation of IWinter, a Nodejs library that converts routing to controller. I think it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

IWinter is a node library that converts routes into controllers. It only solves one problem: to allow users to write routes in a more elegant manner. Supported for use in Express and Koa.

Project address: https://github.com/yvanwangl/iwinter

Introduction:

I am learning to use Typescript recently and plan to write it in the past The blog management backend was completely reconstructed using Typescript. When reconstructing the server code, I came into contact with the decorator function of Typescript. You can use a decorator to wrap the route, and then you can write the route like this:


import {Path, GET, POST, PathParam, BodyParam} from 'iwinter';

@Path('/api/orders')
class OrdersController {

  @GET
  @Path('/:name/:id', (ctx, next)=> ~~ctx.params.id > 20)
    getAllOrders(@PathParam('id') id: number, @PathParam('name') name: string){
    return [{
        id: id, name, content: 'test', author: 'test', comments: []
      }];
  }

  @POST
  @Path('/add')
  addPost(@BodyParam('order') order: object){
    return order;
  }
}

export default OrdersController;

Since you will use Java to write control layer code (Spring MVC control layer code), so seeing such code is so kind. Moreover, compared with the previous way of writing routes, it is clearer and easier to maintain, so the original code was transformed. After I used it, I found it to be very useful, and I wanted more people to use it, so the library IWinter was born. IWinter encapsulates various decorators or decorator factory functions and exposes some APIs for everyone to use. Say goodbye to writing routes like this from now on;)


//app.js
...
let users = require('./routes/users');
let orders = require('./routes/orders');

app.use('/api/users', users);
app.use('/api/orders', orders);
...

//routers/orders.js
...
router.route('/')
  .get(function (req, res, next) {
    let {page, timeRange, customerId, orderNumber} = req.query;
    let limit = constants.PAGE_SIZE;
    let skip = (page - 1) * limit;
    let currentUser = global[Symbol.for('currentUser')];
    let queryCondition = {
      userId: currentUser['_id']
    };
      ...

Who is suitable to use IWinter:

  1. Nodejs users

  2. Typescript User

  3. Express / Koa User

IWinter Installation and Use


npm install --save iwinter


import IWinter from 'iwinter';

How to use in Koa:


##

import * as Router from 'koa-router';
...
app.use(new IWinter({
    engine: 'koa',
  router: new Router(),
  dir: path.join(__dirname, 'controller')
}).controller());
...

How to use in Express:


import * as express from 'express';
let app = express();
let router = express.Router();
...
new IWinter({
  engine: 'express',
  router: app,
  dir: path.join(__dirname, 'controller'),
    prefix: ''
}).controller();

Related recommendations:


Node.js design pattern uses streams for encoding

The environment variable process.env in Node.js will be explained in detail first

Detailed explanation of the event loop in JS and Node.js

The above is the detailed content of Detailed explanation of IWinter, a routing-to-controller Nodejs library. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn