mock in ember project

by hamzeen hameem

HTML

<h3>1. genarate ember project with ember-cli</h3>
<h3>2. it comes with expressjs (hhtp-mock); run </h3>
<pre>ember g http-mock destinations</pre>
<h3>3. to view: http://localhost:4200/api/destinations</h3>
<h3>ref: </h3>
<ul><li>https://discuss.emberjs.com/t/how-i-use-ember-cli-with-http-mock/7403</li>
<li>Discourse uses ember.js</li></ul>
<h3>4. setting up a mock response</h3>
<pre>
/*jshint node:true*/
module.exports = function(app) {
  var express = require('express');
  var destinationsRouter = express.Router();

  destinationsRouter.get('/', function(req, res) {
    res.send({
      'destinations': [
		{
			id:'001',
			loc: 'colombo'
		}, {
			id:'002', 
			loc: 'singapore'
		}, {
			id:'003', 
			loc: 'kuala lumpur'
		}
	  ]
    });
  });

  destinationsRouter.post('/', function(req, res) {
    res.status(201).end();
  });

  destinationsRouter.get('/:id', function(req, res) {
    res.send({
      'destinations': {
        id: req.params.id
      }
    });
  });

  destinationsRouter.put('/:id', function(req, res) {
    res.send({
      'destinations': {
        id: req.params.id
      }
    });
  });

  destinationsRouter.delete('/:id', function(req, res) {
    res.status(204).end();
  });

  // The POST and PUT call will not contain a request body
  // because the body-parser is not included by default.
  // To use req.body, run:

  //    npm install --save-dev body-parser

  // After installing, you need to `use` the body-parser for
  // this mock uncommenting the following line:
  //
  //app.use('/api/destinations', require('body-parser').json());
  app.use('/api/destinations', destinationsRouter);
};

</pre>