Koa-based routing middleware

  • The API is compatible with KOA-Router. For details about the interface, see API Reference
  • Annotation-based route writing is supported, facilitating restful implementation
  • Built-in KOA-body for easy parsing of parameters

The installation

As you can see, dolphin-Router has been published in the NPMJS central repository and can be installed and used directly

Install as a dependency
npm i dolphin-router --save
Copy the code

Koa-based Web development example

New KOA project

To perform the following operations, install the Node. js running environment in advance

If you have Windows, use the relevant shell to run git bash

Create a directory
mkdir koatest

Initialize the Node project. For convenience, all options default
npm init -y

Install the KOA framework and Dolphin-Router
npm i koa dolphin-router --save

# ok project created
Copy the code

Dolphin-router example of koA-Router compatibility

Create the index.js file in the root of the above folder koatest

touch index.js
Copy the code

Add the following code to the file

// Introduce koa const koa = require('koa') // DPRouter doesn't need to create const DPRouter = require('dolphin-router') // Create koa application const app = new koa () // Add routing rules dprouter. Get ('/', async (CTX) => {ctx.body = 'Hello Dolphin Router! Use (dprouter.routes()).use(dprouter.allowedMethods()) () => {console.log(' Please visit http://localhost:3000/ to test... ')})Copy the code

Annotation-based routing (recommended)

Dolphin-router is only used as koA-router in the above example. This middleware is advocating annotation writing. Follow the steps below to write a small demo

  1. Create a path in the root directorysrc/controllersNote that this is the default path of dolphin-router, which will be directly resolved by the routing middleware when the application starts (see configuration below).
  2. Create a file demoController.js with an extension of.js. Note that there is only one class in a Controller, as shown below
Const Koa = require(' Koa ') const dprouter = require('dolphin-router') const app = new Koa() // App.use (dprouter.routes()) app.use(dprouter.allowedmethods ()) app.listen(3000, () => {console.log(' started successfully ')})Copy the code

The code for demoController.js is shown below

Module.exports = class DemoController {/** * @api {get} / hello */ async hello(CTX) {// simply return CTX. Body = 'hello Dolphin'  Router! ' } /** * @api {post} / haha */ async haha(ctx) { ctx.body = ctx.request.body } }Copy the code

The PUT and delete methods are similar.

  1. If you need to specify the path of the project controller, add a few lines of configuration to package.json. Details are as follows:
"dolphin": {
    "controller": {
      "path": "./src/controller"}}Copy the code

Dolphin, Controller, and Path cannot be changed. The value of path is the path to the Controller folder relative to the project root directory

Ok, so how do we get the Query argument, the body argument, and the file? So easy!

HTTP request parameter parsing

  • Query ctx.query or ctx.request.query

  • If we were using the KOA-Router for the body, we might need to introduce middleware that parses the body, such as the KOA-body. This is a good Body Parser, so Dolphin integrates this and gives the default configuration. Use as follows:

    Use (dprouter.koabody ()) // End app.use(dprouter.routes())...Copy the code

    Now that you can parse the body, you can get the data you received from ctx.request.body and the files you uploaded from ctx.request.files.

  • By default, files are saved to the program’s run directory. If there are other configurations for koa-body, you can refer to the koa-body instructions in the app.use(dprouter. KoaBody ({yourself option… })) to configure.

Here is an example

Module. exports = class DemoController {/** * instance demo unchanged * @api {post} / haha */ async haha(CTX) {// Get file address console.log(ctx.request.query) console.log(ctx.request.body) console.log(ctx.request.files) ctx.body = ctx.request.body }}Copy the code

Now that you have finished your initial experience with dolphin-Router, other features are under development. In the future, you will be able to provide parameter verification, automatic API document generation and other features.

📕 Receiving benefits

Use your hands, follow the public account, reply to node. js tutorial to get the beginner’s course!