koa - 带有 koa2 的 REST API。多个路由器的公共(public)前缀

标签 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'
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 是一样的)。
// Export a function that takes the router
export default router => {
  // Set a prefix of our api, in this case locations
  const api = 'users'
  router.prefix(`/${api}`);

  // GET to all locations.
  router.get('/',  (ctx, next) =>
      ctx.body = 'hello users');
    // ctx.body = await Location.find());
  // POST a new location.
  router.post('/', async (ctx, next) =>
    ctx.body = await new Location(ctx.request.body).save());
  // Routes to /locations/id.
  router.get('/:id', async (ctx, next) =>
    ctx.body = await Location.findById(ctx.params.id));
  // PUT to a single location.
  router.put('/:id', async (ctx, next) =>
    ctx.body = await Location.findByIdAndUpdate(ctx.params.id, ctx.body));
  // DELETE to a single location.
  router.delete('/:id', async (ctx, next) =>
    ctx.body = await Location.findByIdAndRemove(ctx.params.id));

  return router;
}

最佳答案

我使用以下解决方案:

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());

关于koa - 带有 koa2 的 REST API。多个路由器的公共(public)前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35302560/

相关文章:

javascript - 编写 koa 异步函数中间件时出错

javascript - 将大型 koa 请求正文上传到 AWS S3 的正确方法是什么?

node.js - 来自远程 url 的 koa.js 流响应

node.js - 如何获取 Koa 服务器 url 路由列表

node.js - 使用 request-promise 时无法设置 Koajs 的 ctx.status 和 ctx.body

node.js - 如何使用 async/await 和 Promise 响应?

javascript - Koa JS - 调用 API 后无法发送响应。总是收到 404

javascript - 如何利用 Request 来集成测试异步 Koa Node API

node.js - 如何在 Openshift 中设置 KoaJS

javascript - 你如何在 promise 或回调中运行 `yield next`?