-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
54 lines (45 loc) · 1.3 KB
/
server.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
var http = require("http");
var store = "";
setInterval(recache, 30000);
recache();
function recache() {
var options = {
host: 'api.ihackernews.com',
port: 80,
path: '/page'
};
http.get(options, function(res) {
var body = "";
res.on('data', function(chunk){body += chunk;});
res.on('end', function(){
try {
store = JSON.parse(body)['items'];
for(var i in store) {
store[i].camel = store[i].title.toCamelCase().replace(/[\.\'–—\+’,-\/#!$%\^&\*;:{}=\-_`~()]/g,"");
store[i].camel = store[i].camel[0].toLowerCase() + store[i].camel.slice(1);
//store[i].description = store[i].description.replace("Comments", "comments");
}
} catch (e) {}
});
});
}
var express = require("express");
var app = express.createServer();
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
app.set('view options', {
layout: false
});
app.get('/', function(req, res){
res.render('index', {store: store});
});
app.listen(8081);
String.prototype.toCamelCase = function() {
return this.toString()
.replace(/([A-Z]+)/g, function(m,l){
return l.substr(0,1).toUpperCase() + l.toLowerCase().substr(1,l.length);
})
.replace(/[\-_\s](.)/g, function(m, l){
return l.toUpperCase();
});
};