-
Notifications
You must be signed in to change notification settings - Fork 0
/
express.js
53 lines (43 loc) · 1.53 KB
/
express.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var express = require('express')
, mongoskin = require('mongoskin')
var app = express()
app.use(express.bodyParser())
var db = mongoskin.db('localhost:27017/test', {safe:true});
app.param('collectionName', function(req, res, next, collectionName){
req.collection = db.collection(collectionName)
return next()
})
app.get('/', function(req, res, next) {
res.send('please provide a valid api path, e.g., /api/some_valid_api')
})
app.get('/api/:collectionName', function(req, res, next) {
req.collection.find({},{limit:10, sort: [['_id',-1]]}).toArray(function(e, results){
if (e) return next(e)
res.send(results)
})
})
app.post('/api/:collectionName', function(req, res, next) {
req.collection.insert(req.body, {}, function(e, results){
if (e) return next(e)
res.send(results)
})
})
app.get('/api/:collectionName/:id', function(req, res, next) {
req.collection.findOne({_id: req.collection.id(req.params.id)}, function(e, result){
if (e) return next(e)
res.send(result)
})
})
app.put('/api/:collectionName/:id', function(req, res, next) {
req.collection.update({_id: req.collection.id(req.params.id)}, {$set:req.body}, {safe:true, multi:false}, function(e, result){
if (e) return next(e)
res.send((result===1)?{msg:'success'}:{msg:'error'})
})
})
app.del('/api/:collectionName/:id', function(req, res, next) {
req.collection.remove({_id: req.collection.id(req.params.id)}, function(e, result){
if (e) return next(e)
res.send((result===1)?{msg:'success'}:{msg:'error'})
})
})
app.listen(3000)