-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
53 lines (48 loc) · 1.21 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
/**
* Load environment variables from .env
*/
require('dotenv').load();
/**
* Module dependencies
*/
var express = require('express')
, storage = require('./server/storage')
, dictionary = require('./server/dictionary')
, rhymes = require('./server/rhymes')
, twitter = require('./server/twitter')
, app = express();
/**
* Configure app
*/
app.set('views', __dirname + '/views');
app.use(express.favicon());
app.use(express.static(__dirname + '/client'));
app.use(express.bodyParser());
/**
* Set main API end point. Responds with rhyming tweets when POSTed a line.
*/
app.post('/rhyme', function (req, res) {
var line = req.body.line
, targetWord = line.substring(line.lastIndexOf(' ') + 1);
rhymes.find(targetWord)
.then(storage.find)
.then(res.json.bind(res))
.catch(function(err) {
console.error('[REQUEST_FAILURE]', err.stack);
res.send(500, '!?');
});
});
/**
* Initialize application and start listening on port
*/
dictionary.loadWordSet()
.then(twitter.startStreaming)
.then(function() {
var port = process.env.PORT || 3000;
console.log('Listening on port: ' + port);
app.listen(port);
})
.catch(function(err) {
console.error('[APP_FAILURE]', err.stack);
process.exit(1);
});