Last active
August 29, 2015 14:13
-
-
Save TheIronDev/69de8b6273586f155d2b to your computer and use it in GitHub Desktop.
A dummy in-memory store mock ember-cli example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(app) { | |
var express = require('express'); | |
var postsRouter = express.Router(); | |
var posts = [], | |
count = 1; | |
postsRouter.get('/', function(req, res) { | |
res.send({ | |
'posts': posts | |
}); | |
}); | |
postsRouter.post('/', function(req, res) { | |
var newPost = { | |
id: count, | |
title: '', | |
description: '' | |
}; | |
req.on("data",function(chunk){ | |
var thing = JSON.parse(chunk.toString()), | |
post = thing.post; | |
newPost.title = post.title; | |
newPost.description = post.description; | |
}); | |
req.on("end",function(){ | |
res.send({ | |
post: newPost | |
}); | |
posts.push(newPost); | |
count++; | |
}); | |
}); | |
postsRouter.get('/:id', function(req, res) { | |
res.send({ | |
'posts': { | |
id: req.params.id | |
} | |
}); | |
}); | |
postsRouter.put('/:id', function(req, res) { | |
res.send({ | |
'posts': { | |
id: req.params.id | |
} | |
}); | |
}); | |
postsRouter.delete('/:id', function(req, res) { | |
res.status(204).end(); | |
}); | |
app.use('/api/posts', postsRouter); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment