Skip to content

Instantly share code, notes, and snippets.

Created October 24, 2014 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9ab8853de7f7661f88ac to your computer and use it in GitHub Desktop.
Save anonymous/9ab8853de7f7661f88ac to your computer and use it in GitHub Desktop.
var express = require('express'),
Bourne = require('bourne'),
bodyParser = require('body-parser'),
db = new Bourne('data.json'),
router = express.Router();
router
.use(function (req, res, next){
if(!req.user) req.user = { id:1 };
next();
})
.use(bodyParser.json())
.route('/contacts')
.get(function (req, res){
db.find({ userId: parserInt(req.user.id,10)}, function (err,data){
res.json(data)
});
})
.post(function (req,res){
var contact = req.body;
contact.userId = req.user.id;
db.insert(contact, function(err,data){
res.json(data);
});
});
router.
.param('id', function( req,res,next){
req.dbQuery = { id: parseInt(req.params.id,10)}
})
.route('/contact/:id')
.get(function (req,res){
db.findOne(req.dbQuery, function(err,data){
res.json(data);
});
})
.put(function (req, res){
var contact = req.body;
delete contact.$promise;
delete contact.$resolved;
db.update(req.dbQuery,contact, function(err, data){
res.json(data[0]);
});
})
.delete(function (req, res){
db.delete(req.dbQuery,function(){
res.json(null);
});
});
module.exports = router;
var express = require('express'),
api = require('./api'),
app = express();
app
.use(express.static('./public'))
.use('/api',api)
.get('*', function (req, res) {
res.sendfile('public/main.html');
})
.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment