-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (41 loc) · 1.13 KB
/
app.js
File metadata and controls
48 lines (41 loc) · 1.13 KB
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
var express = require('express'),
app = express(),
expressLayouts = require('express-ejs-layouts');
app.listen(8000);
app.set('view engine', 'ejs');
app.use(expressLayouts);
var tweets = [];
app.get('/', function(req, res) {
var title = 'Chirpie',
header = 'Welcome to Chirpie';
res.render('index', {
locals: {
'title': title,
'header': header,
'tweets': tweets,
stylesheets: ['/public/style.css']
}
})
});
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 {
res.send({status:"nok", message:"No tweet received"});
}
});
app.get('/tweets', function(req, res) {
res.send(tweets);
});
function acceptsHtml(header) {
var accepts = header.split(',');
for(var i = 0; i < accepts.length; i += 1) {
if (accepts[i] === 'text/html') { return true; }
}
return false;
}