Skip to content

Instantly share code, notes, and snippets.

@marcusoftnet
Created March 29, 2014 12:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusoftnet/9853669 to your computer and use it in GitHub Desktop.
Save marcusoftnet/9853669 to your computer and use it in GitHub Desktop.
Post and get data from Mongo via Koa
// Create the application
var koa = require('koa');
var route = require('koa-route');
var parse = require('co-body');
var app = module.exports = koa();
// Get monk up an running
var monk = require('monk');
var wrap = require('co-monk');
var db = monk('localhost/koausers');
var users = wrap(db.get('users'));
// Set up some routes
app.use(route.post('/user', create));
app.use(route.get('/user', list));
// And here is the handling code
function *create() {
// Parse the user from the posted data
// using the co-body package
var user = yield parse(this);
user.created_on = new Date;
// Store it in database
try
{
this.body = yield users.insert(user);
this.status = 201;
}
catch(e)
{
this.body = "An error occured: " + e;
this.status = 401;
}
};
function *list() {
var res = yield users.find({});
this.body = res;
};
// fire it up
app.listen(3000);
console.log("Post to http://localhost:3000/user to store new stuff");
console.log("Get from http://localhost:3000/user to see your users");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment