Last active
November 29, 2015 07:45
-
-
Save tlan16/c66fa1526545c14aca2b to your computer and use it in GitHub Desktop.
nodejs_orm.js
This file contains 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
var express = require('express'); | |
var orm = require("orm"); | |
var app = express(); | |
var bodyParser = require('body-parser'); | |
var port = process.env.PORT || 8080; | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(bodyParser.json()); | |
var db = orm.connect('mongodb://localhost/docman'); | |
db.load("./app/models/person", function (err) { | |
var Person = db.models.person; | |
}); | |
var router = express.Router(); | |
//middleware | |
router.use(function(req, res, next) { | |
// log | |
console.log('Something is happening.'); | |
next(); // make sure we go to the next routes and don't stop here | |
}); | |
router.route('/person') | |
// create | |
.post(function(req, res) { | |
var obj = new Person(); | |
obj.name = req.body.name; | |
obj.save(function(err) { | |
if (err) | |
res.send(err); | |
res.json({ message: obj }); | |
}); | |
}) | |
// get | |
.get(function(req, res) { | |
Person.find(function(err, objs) { | |
if (err) | |
res.send(err); | |
res.json(objs); | |
}); | |
}); | |
app.use('/api', router); | |
app.listen(port); | |
console.log('App started on port ' + port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment