-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (44 loc) · 1.14 KB
/
app.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
/**
* Module dependencies.
*/
var express = require('express');
var app = express();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use('/public', express.static(__dirname + '/public'));
});
// Routes
app.get('/', function(req, res) {
var title = 'Switter',
header = 'Welcome to Switter';
res.render('index', {
'title': title,
'header': header,
'tweets': tweets,
})
})
var tweets = [];
app.get('/tweets', function(req,res) {
res.send(tweets);
})
app.post('/send', express.bodyParser(), function(req, res) {
if (req.body && req.body.tweet) {
tweets.push(req.body.tweet);
if(acceptsHtml(req.headers['accept'])) {
res.redirect('/', 302)
} else {
res.send({status:"ok", message:"Tweet received"});
};
} else {
//no tweet?
res.send({status:"nok", message:"No tweet received"});
};
});
app.listen(8000);
function acceptsHtml(header) {
var accepts = header.split(',');
for(i=0; i < accepts.length; i++) {
if (accepts[i] === 'text/html') { return true; };
}
return false;
}