-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeJS-mongo.js
99 lines (94 loc) · 2.64 KB
/
nodeJS-mongo.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
/***** HTTP layer *****/
var SYS = require('sys');
var HTTP = require('http');
var URL = require('url');
var FS = require('fs');
var PATH = require('path');
HTTP.createServer(function (request, response) {
var parsedURL = URL.parse(request.url, true)
console.log('->', parsedURL.pathname)
// list tweets
if(parsedURL.pathname == '/tweets' && request.method == 'GET') {
var search = {}
if(parsedURL.query && parsedURL.query.search)
search.message = parsedURL.query.search
require('./mongo').listTweets(search, function(tweets) {
var result = JSON.stringify(tweets)
response.writeHead(200, {
'Content-Length': result.length,
'Content-Type': 'text/json'
})
response.end(result)
})
// create a tweet
} else if(parsedURL.pathname == '/tweets' && request.method == 'POST') {
var obj = undefined
request.on('data', function(data) {
try {
data = data.toString().replace(/\n/g, '\\n')
obj = JSON.parse(data)
obj.date = new Date().getTime()
require('./mongo').createTweet(obj, function() {
response.writeHead(201)
response.end()
})
} catch(e) {
console.log('data were', typeof data, data, 'e=',e)
response.writeHead(500, 'Wrong data: not JSON?')
response.end()
}
})
request.on('end', function() {
if(!obj) {
response.writeHead(500, 'No data?')
response.end();
}
})
// index
} else if(parsedURL.pathname == '/' && request.method == 'GET') {
var view = FS.readFileSync(__dirname + '/index.html')
response.writeHead(200, {
'Content-Type': 'text/html'
})
response.write(view)
response.end()
// static files
} else if(parsedURL.pathname.indexOf('/public/') == 0 && request.method == 'GET') {
var path = __dirname + parsedURL.pathname
path = PATH.normalize(path)
if(path.indexOf(__dirname) != 0) {
response.writeHead(403, 'Forbidden')
response.end()
} else {
PATH.exists(path, function(exists) {
if(exists) {
var head = {}
if(/css$/.test(path)) {
head['Content-Type'] = 'text/css'
}
if(/js$/.test(path)) {
head['Content-Type'] = 'text/javascript'
}
if(/png$/.test(path)) {
head['Content-Type'] = 'image/png'
}
if(/(jpg|jpeg)$/.test(path)) {
head['Content-Type'] = 'image/jpeg'
}
console.log('served static file', path, 'with head', SYS.inspect(head))
response.writeHead(200, head)
response.write(FS.readFileSync(path))
response.end()
} else {
response.writeHead(404)
response.end()
}
})
}
// not found.
} else {
response.writeHead(404, 'Not Found!!')
response.end()
}
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');