-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathweb.js
61 lines (59 loc) · 2.26 KB
/
web.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
const http = require('http')
const util = require('util')
const querystring = require('querystring')
const client = require('mongodb').MongoClient
const uri = process.env.MONGOLAB_URI || 'mongodb://@127.0.0.1:27017/messages'
//MONGOLAB_URI=mongodb://user:pass@server.mongohq.com:port/db_name
client.connect(uri, function(error, db) {
if (error) return console.error(error)
const collection = db.collection('messages')
const app = http.createServer(function (request, response) {
const origin = (request.headers.origin || '*')
if (request.method == 'OPTIONS') {
response.writeHead('204', 'No Content', {
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Methods':
'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'content-type, accept',
'Access-Control-Max-Age': 10, // In seconds
'Content-Length': 0
})
response.end()
} else if (request.method === 'GET' && request.url === '/messages.json') {
collection.find().toArray(function(error,results) {
if (error) return console.error(error)
const body = JSON.stringify(results)
response.writeHead(200,{
'Access-Control-Allow-Origin': origin,
'Content-Type':'text/plain',
'Content-Length':body.length
})
console.log('LIST OF OBJECTS: ')
console.dir(results)
response.end(body)
})
} else if (request.method === 'POST' && request.url === '/messages.json') {
request.on('data', function(data) {
console.log('RECEIVED DATA:')
console.log(data.toString('utf-8'))
collection.insert(JSON.parse(data.toString('utf-8')),
{safe:true}, function(error, obj) {
if (error) return console.error(error)
console.log('OBJECT IS SAVED: ')
console.log(JSON.stringify(obj))
const body = JSON.stringify(obj)
response.writeHead(200,{
'Access-Control-Allow-Origin': origin,
'Content-Type':'text/plain',
'Content-Length':body.length
})
response.end(body)
})
})
} else {
response.end('Supported endpoints: \n/messages.json\n/messages.json')
}
})
const port = process.env.PORT || 1337
app.listen(port)
})