Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. // Dependencies
  2. import Koa from 'koa'
  3. import mongoose from 'mongoose'
  4. import logger from 'koa-logger'
  5. // import parser from 'koa-bodyparser';
  6. import convert from 'koa-convert'
  7. import serve from 'koa-static'
  8. import Router from 'koa-router'
  9. import session from 'koa-generic-session'
  10. import mount from 'koa-mount'
  11.  
  12. // A seperate file with my routes.
  13. import routingUsers from './users'
  14. import routingEmployees from './employees'
  15.  
  16. // config
  17. const config = require("./config/config")
  18.  
  19. // connect to the database
  20. mongoose.connect(config.mongo.url)
  21. mongoose.connection.on('error', console.error)
  22.  
  23. // Creates the application.
  24. const app = new Koa()
  25. // how to use koa-mount to make this work? Arghhhhh!
  26. // const api = new Koa();
  27. // api.use(convert(mount ('/api', app)))
  28.  
  29. // trust proxy
  30. app.proxy = true
  31. // sessions
  32. app.keys = ['your-session-secret']
  33.  
  34.  
  35. // Applies all routes to the router.
  36. const user = routingUsers(Router())
  37. const employee = routingEmployees(Router())
  38.  
  39. app
  40. .use(logger()) // log requests, should be at the beginning
  41. .use(user.routes()) // asign routes
  42. .use(employee.routes()) // asign routes
  43. .use(user.allowedMethods())
  44. .use(employee.allowedMethods())
  45. .use(convert(session())) // session not needed for an API??????
  46. .use(convert(serve(__dirname + '/public'))) // for static files like images
  47.  
  48.  
  49. // Start the application.
  50. app.listen(3000, () => console.log('server started 3000'))
  51. export default app
  52.  
  53. // Export a function that takes the router
  54. export default router => {
  55. // Set a prefix of our api, in this case locations
  56. const api = 'users'
  57. router.prefix(`/${api}`);
  58.  
  59. // GET to all locations.
  60. router.get('/', (ctx, next) =>
  61. ctx.body = 'hello users');
  62. // ctx.body = await Location.find());
  63. // POST a new location.
  64. router.post('/', async (ctx, next) =>
  65. ctx.body = await new Location(ctx.request.body).save());
  66. // Routes to /locations/id.
  67. router.get('/:id', async (ctx, next) =>
  68. ctx.body = await Location.findById(ctx.params.id));
  69. // PUT to a single location.
  70. router.put('/:id', async (ctx, next) =>
  71. ctx.body = await Location.findByIdAndUpdate(ctx.params.id, ctx.body));
  72. // DELETE to a single location.
  73. router.delete('/:id', async (ctx, next) =>
  74. ctx.body = await Location.findByIdAndRemove(ctx.params.id));
  75.  
  76. return router;
  77. }
  78.  
  79. // Applies all routes to the router.
  80. const user = routingUsers(Router(), 'api/users/')
  81. const employee = routingEmployees(Router(), 'api/employees/')
  82.  
  83. export default (router, prefix) => {
  84. // Set a prefix of our api, in this case locations
  85. // const api = 'users'
  86. router.prefix(`/${prefix}`);
  87. ....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement