'use strict';

const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const Twitter = require('twitter');
const watson = require('watson-developer-cloud');

const routes = require('./routes/index');
const users = require('./routes/users');

const app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

const client = new Twitter({
  consumer_key: process.env.COFFEE_PERSONALITY_TWITTER_CONSUMER_KEY,
  consumer_secret: process.env.COFFEE_PERSONALITY_TWITTER_CONSUMER_SECRET,
  access_token_key: process.env.COFFEE_PERSONALITY_TWITTER_ACCESS_TOKEN_KEY,
  access_token_secret: process.env.COFFEE_PERSONALITY_TWITTER_ACCESS_TOKEN_SECRET
});

app.get('/api/getname/:name', function(req, res) {
  const params = { screen_name: req.params.name, count: 1000 };
  client.get('statuses/user_timeline', params, function(error, tweets) {
    if (!error) {
      res.json(tweets);
    }
  });
});

const personality_insights = watson.personality_insights({
  username: process.env.COFFEE_PERSONALITY_INSIGHTS_USERNAME,
  password: process.env.COFFEE_PERSONALITY_INSIGHTS_PASSWORD,
  version: 'v2'
});

app.put('/api/watson/:twitteruser', function(req, res) {
  const username = req.params.twitteruser;

  const watsonFetch = function() {
    return new Promise(function(resolve) {
      const params = { screen_name: username, count: 5000 };
      client.get('statuses/user_timeline', params, function(error, tweets) {
        if (!error) {
          let test;
          for (let i = 0; i < tweets.length; i++) {
            console.log(tweets[i].text);
            test += tweets[i].text;
          }
          resolve(test);
        }
      });
    });
  };

  watsonFetch()
    .then(function(result) {
      personality_insights.profile({
        text: result,
        language: 'en'
      }, function(err, response) {
        if (err) {
          console.log('error:', err);
        } else {
          const array = [];
          const i = response.tree.children;
          i.forEach(function(thing) {
            thing.children.forEach(function(thing2) {
              const obj = { id: thing2.id, percentage: thing2.percentage };
              array.push(obj);
              thing2.children.forEach(function(thing4) {
                const obj = { id: thing4.id, percentage: thing4.percentage };
                array.push(obj);
              });
            });
          });
          console.log(array);
          res.json(array);
        }
      });
    })
    .catch(function(e) {
      console.log(e);
    });
});

// error handlers

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  const err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

module.exports = app;

@@ -1053,255 +1053,8 @@
 );%0A%0A
-app.get('/api/getname/:name', function(req, res) %7B%0A  const params = %7B screen_name: req.params.name, count: 1000 %7D;%0A  client.get('statuses/user_timeline', params, function(error, tweets) %7B%0A    if (!error) %7B%0A      res.json(tweets);%0A    %7D%0A  %7D);%0A%7D);%0A%0A
 cons

Remove unused GET route from app