Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. const { send, json } = require('micro')
  2. const { router, get, post } = require('microrouter')
  3. const cors = require('micro-cors')()
  4.  
  5. const contacts = [{ "name": "Tyler" }, { "name": "Wes" }, { "name": "David" }]
  6.  
  7. const getContacts = async (req, res) => {
  8. console.log(contacts)
  9. send(res, 200, contacts)
  10. }
  11.  
  12. const createContact = async (req, res) => {
  13. const body = await json(req)
  14. contacts.push(body)
  15. send(res, 200, body)
  16. }
  17.  
  18. const hello = (req, res) => send(res, 200, `Hello ${req.params.who}`)
  19.  
  20. const notfound = (req, res) => send(res, 404, 'Not found route')
  21.  
  22. module.exports = cors(
  23. router(
  24. get('/contacts', getContacts),
  25. post('/contacts', createContact),
  26. get('/hello/:who', hello),
  27. get('/*', notfound)
  28. )
  29. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement