ember.js - ember-cli mocks doesn't return single record -
i'm building simple ember-cli app. , tried implement new mocks feature.
server/mocks/users.js
module.exports = function (app) { var express = require('express'); var usersrouter = express.router(); usersrouter.get('/', function (req, res) { res.send({"users": [ { "id": 1, "login": 'berdof' }, { "id": 2, "login": 'berdof2' } ]}); }); app.use('/api/users', usersrouter); };
next when i'm trying fetch data, instance in browser, open
http://0.0.0.0:4200/api/user/
and correct data. when want fetch single record throws me error
cannot /api/users/1
do know why's that? i've been fighting bug 2 hours. help! have day!
update
here's modified answer according @kaungst post:
module.exports = function (app) { var express = require('express'); var router = express.router(); var datatosend = [ { id: 1, login: 'berdof', }, { id: 1, login: 'berdof2' } ]; router.get('/', function (req, res) { res.send({"users": datatosend}); }); router.get('/:id', function (req, res) { var id = req.params.id; res.send({"user": datatosend[id]}); }); app.use('/api/users', router); };
you have create additional mock handle case
for specific example run
ember g http-mock user
which should generate following in <\app-name>/server/mocks/user.js
module.exports = function (app) { var express = require('express'); var userrouter = express.router(); userrouter.get('/', function (req, res) { res.send({"user": []}); }); app.use('/api/user', userrouter); };
alter this
module.exports = function (app) { var express = require('express'); var userrouter = express.router(); userrouter.get('/', function (req, res) { // part hacky works // main thing decide want send // happy update if knows better solution var url = req.baseurl.split('/'); var id = parseint(url[url.length-1]); var users = [{ "id": 1, "login": 'berdof' }, { "id": 2, "login": 'berdof2' }]; res.send({"user": users[id]}); }); app.use('/api/user/:id', userrouter); };
Comments
Post a Comment