-
Notifications
You must be signed in to change notification settings - Fork 0
/
express.js
117 lines (94 loc) · 3.54 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var express = require('express'),
//cors = require('cors'),
mongoskin = require('mongoskin'),
bodyParser = require('body-parser'),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
db_uri = process.env.PROD_MONGODB || 'mongodb://@localhost:27017/test',
port = process.env.PORT || 3000;
var app = express()
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
// res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3001');
res.setHeader('Access-Control-Allow-Origin', 'http://sdellis.com');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.sendStatus(204);
}
else {
next();
}
});
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(logger('dev'))
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(__dirname + '/public/favicon.ico'));
var db = mongoskin.db(db_uri, {safe:true})
var corsOptions = {
origin: '*',
methods: 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
allowedHeaders: 'Origin, X-Requested-With, Content-Type, Accept'
};
app.options('/collections/:collectionName/:id', cors()); // include before other routes
app.param('collectionName', function(req, res, next, collectionName){
req.collection = db.collection(collectionName)
return next()
})
app.get('/', function(req, res, next) {
//res.send('please select a collection, e.g., /collections/messages')
res.sendfile('/interface.html');
path = req.params[0] ? req.params[0] : 'index.html';
res.sendfile(path, {root: './public/'});
})
app.get('/collections/: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.get('/collections/:collectionName/:id', function(req, res, next) {
req.collection.findById(req.params.id, function(e, result){
if (e) return next(e)
res.send(result)
})
})
app.post('/collections/:collectionName', function(req, res, next) {
// if an @id is supplied use the post-prefix string as the _id and add it to the manifest
if (typeof req.body["@id"] !== 'undefined') {
var arr = req.body["@id"].split("/");
var _id = arr[arr.length-1];
req.body["_id"] = _id;
}
req.collection.insert(req.body, {}, function(e, results){
if (e) return next(e)
res.send(results)
})
})
app.put('/collections/:collectionName/:id', function(req, res, next) {
req.collection.updateById(req.params.id, {$set: req.body}, {safe: true, multi: false}, function(e, result){
if (e) return next(e)
//res.status(204).send()
res.send((result === 1) ? {msg:'success'} : {msg: 'error'})
})
})
app.delete('/collections/:collectionName/:id', function(req, res, next) {
req.collection.removeById(req.params.id, function(e, result){
if (e) return next(e)
res.send((result === 1)?{msg: 'success'} : {msg: 'error'})
})
})
app.listen(port, function(){
console.log('Express server listening on port ' + port)
})
*/