Advertisement
tdudzik

Untitled

Jan 12th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const app = express();
  3.  
  4. const products = [{
  5.   id: 'id-111',
  6.   name: 'Name 1',
  7.   price: 50
  8. }, {
  9.   id: 'id-222',
  10.   name: 'Name 2',
  11.   price: 40
  12. }];
  13.  
  14. const resources = [{
  15.   path: '/products',
  16.   responses: [{
  17.     method: 'GET',
  18.     status: 200,
  19.     data: products,
  20.   }, {
  21.     method: 'POST',
  22.     status: 201,
  23.     data: products[0]
  24.   }]
  25. }];
  26.  
  27. resources.forEach((resource) => {
  28.   resource.responses.forEach((response) => {
  29.     switch (response.method) {
  30.       case 'GET':
  31.         app.get(resource.path, (req, res) => {
  32.           res.status(response.status).send(response.data);
  33.         });
  34.         break;
  35.       case 'POST':
  36.         app.post(resource.path, (req, res) => {
  37.           res.status(response.status).send(response.data);
  38.         });
  39.         break;
  40.     }
  41.   });
  42. });
  43.  
  44. app.listen(8080);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement