This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
app.js
72 lines (62 loc) · 1.8 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var app = require('express')();
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
// Configure Express
app.set('view engine', 'ejs');
require('ejs').delimiter = '$';
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, './public')));
var profileByUser = {};
var watson = require('watson-developer-cloud');
var personalityInsights = watson.personality_insights({ version: 'v2' });
var processProfile = require('./util/process-personality-profile');
// render index page
app.get('/', function(req, res) {
res.render('tweets');
});
/**
* Returns the personality Insights from a username.
*/
app.get('/api/profile', function(req, res, next) {
if (!req.query.username) {
return next('Missing required query parameter: "username"');
}
var profile = profileByUser[req.query.username];
if (profile) {
return res.json(profile);
}
req.app.get('tweeter').getTweets(req.query.username, function (error, tweets) {
if (error) {
return next(next);
}
personalityInsights.profile({ contentItems:tweets }, function (perror, profile) {
if (error) {
return next(perror);
}
var processedProfile = processProfile(profile);
profileByUser[req.query.username] = processedProfile;
res.json(processedProfile);
});
});
});
app.get('/tweets', function(req, res) {
res.render('tweets');
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.code = 404;
err.message = 'Not Found';
next(err);
});
// error handler
// eslint-disable-next-line
app.use(function(err, req, res, next) {
var error = {
code: err.code || 500,
error: err.error || err.message
};
res.status(error.code).json(error);
});
module.exports = app;