0

I have a node.js (with express) module that connects to twitter and streams data based on search terms the user inputs in the front end (see: twitter.tweetStream(api, params, values, track, bridgeArray, usernameArray, scoreData);). The module executes when the user submits a form and is directed to /test, but it continues to run even after they leave /test. It also runs in parallel with any new instance of it that starts.

Is there a way to tell the module to stop running if the user leaves the /test route? Th

// Renders form on default Route
app.get('/', function(req, res){
    res.render('form');

});

// On form submission take data and passes it into twitter stream call as "track" object then renders the 'tweets' feed
app.post('/test',function(req,res){
    var track = req.body;
    twitter.tweetStream(api, params, values, track, bridgeArray, usernameArray, scoreData);
    res.render('tweets');
});

// Renders Tweets stored in "values object" onto a page to be called from tweets template
app.get('/tweets', function(req, res){

    res.render('home', {   
        profileImg: values.profileImg,
        userName: values.userName,
        screenName: values.screenName,
        text: values.text,
        timeStamp: values.timeStamp,        
        tweetScore: values.tweetScore,
//        totals: values.totals
    });

});

Complete code added for clarity:

    var express = require('express');
var app = express();
//var http = require('http').Server(app);  // Not using
var exphbs  = require('express-handlebars');
var bodyParser = require('body-parser');
var twitter = require('./twitter.js');
var hue = require("./hue_control.js");

// Variable that control Twitter Calls
var api = 'statuses/filter';
var params = {slug:'REDACTED', owner_screen_name: 'REDACTED', skip_status: true};
var values = {};


app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(bodyParser());
app.use(express.static('public'));


// Checks for Hue bridge on local network gets IP and sets usename
var bridgeArray = [];
var usernameArray = [];
var scoreData = [];
var track = {};

hue.activate(bridgeArray, usernameArray);


// Renders form on default Route
app.get('/', function(req, res){
    res.render('form');
});

// On form submission take data and passes it into twitter stream call as "track" object then renders the 'tweets' feed
app.post('/tweets',function(req,res){
    track = req.body;
    res.render('tweets');
    twitter.tweetStream(api, params, values, track, bridgeArray, usernameArray, scoreData);    
});

// Renders Tweets stored in "values object" onto a page to be called from tweets template
app.get('/tweetstatic', function(req, res){
    res.render('home', {   
        profileImg: values.profileImg,
        userName: values.userName,
        screenName: values.screenName,
        text: values.text,
        timeStamp: values.timeStamp,        
        tweetScore: values.tweetScore,
    });
});

app.get('/totals', function(req, res){
    res.render('totals', {   
        tweetTotal: values.tweetTotal,
        score: values.score,
        color: values.color
    });
});

// App Settings
var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});
Rob McCoy
  • 43
  • 1
  • 1
  • 4
  • What is `app`? `app = express()` or `app = express.Router()`? If its the 1st case, use the 2nd. – Swaraj Giri Jul 20 '15 at 14:32
  • Thanks for your response. I'm using: `var app = express();` I also added full code for clarity. – Rob McCoy Jul 20 '15 at 17:58
  • can you link the twitter library? that's where you would find a call to stop the stream – Plato Jul 20 '15 at 23:24
  • @RobMcCoy - anything that you write with the following signature `app.use(function (req, res, next))` becomes an *application* middleware, meaning it runs for every request. What you should be doing for routes is use the `router = express.Router()` object. So instead of `app.get` it becomes `router.get` – Swaraj Giri Jul 21 '15 at 03:33
  • Possible duplicate of [exit from chain of route specific middleware in express/ nodejs](http://stackoverflow.com/questions/17232239/exit-from-chain-of-route-specific-middleware-in-express-nodejs) – Paul Sweatte May 19 '17 at 16:22

0 Answers0