RESTAPI与koa2。几种路由器的通用前缀

RESTAPI与koa2。几种路由器的通用前缀,koa,koa-router,koa2,Koa,Koa Router,Koa2,我有两个实体,用户和员工。因此,我希望在不同的端点中都使用CRUD,但它们都将安装在“api”下,因此我可以定义api_v1、api_v2等等。 端点类似于: get api/users put api/users/12 delete api/users/12 get api/employees .... 我无法获取两条路由的“api”前缀。无法让它与koa mount一起工作 我的文件: server.js // Dependencies import Koa from 'koa' impo

我有两个实体,用户和员工。因此,我希望在不同的端点中都使用CRUD,但它们都将安装在“api”下,因此我可以定义api_v1、api_v2等等。 端点类似于:

get api/users
put api/users/12
delete api/users/12
get api/employees
....
我无法获取两条路由的“api”前缀。无法让它与koa mount一起工作

我的文件:

server.js

// Dependencies
import Koa from 'koa'
import mongoose from 'mongoose'
import logger from 'koa-logger'
// import parser from 'koa-bodyparser';
import convert from 'koa-convert'
import serve from 'koa-static'
import Router from 'koa-router'
import session from 'koa-generic-session'
import mount from 'koa-mount'

// A seperate file with my routes.
import routingUsers from './users'
import routingEmployees from './employees'

// config
const config = require("./config/config")

// connect to the database
mongoose.connect(config.mongo.url)
mongoose.connection.on('error', console.error)

// Creates the application.
const app = new Koa()
// how to use koa-mount to make this work? Arghhhhh!
// const api = new Koa();
// api.use(convert(mount ('/api', app)))

// trust proxy
app.proxy = true
// sessions
app.keys = ['your-session-secret']


// Applies all routes to the router.
const user = routingUsers(Router())
const employee = routingEmployees(Router())

app
  .use(logger()) // log requests, should be at the beginning
  .use(user.routes()) // asign routes
  .use(employee.routes()) // asign routes
  .use(user.allowedMethods())
  .use(employee.allowedMethods())
  .use(convert(session())) // session not needed for an API??????
  .use(convert(serve(__dirname + '/public')))   // for static files like images


// Start the application.
app.listen(3000, () => console.log('server started 3000'))
export default app
users.js(employees.js与之相同)


最后,我向路由器模块发送了另一个参数,因此我使用了路由器前缀:

// Applies all routes to the router.
const user = routingUsers(Router(), 'api/users/')
const employee = routingEmployees(Router(), 'api/employees/')
用户将是:

export default (router, prefix) => {
  // Set a prefix of our api, in this case locations
  // const api = 'users'
  router.prefix(`/${prefix}`);
 ....

我使用以下解决方案:

import Router from 'koa-router';

const router = new Router()
  .get('/', function(ctx) {
    ctx.body = 'Index';
  });


const apiRouter = new Router({
  prefix: '/api'
})
  .get('/templates', Templates.index)
  .post('/templates', Templates.store)
  .put('/templates', Templates.update)
  .get('/lists', Lists.index);


router.use(apiRouter.routes());
import Router from 'koa-router';

const router = new Router()
  .get('/', function(ctx) {
    ctx.body = 'Index';
  });


const apiRouter = new Router({
  prefix: '/api'
})
  .get('/templates', Templates.index)
  .post('/templates', Templates.store)
  .put('/templates', Templates.update)
  .get('/lists', Lists.index);


router.use(apiRouter.routes());