我有一个node.js(带快速)模块,它连接到twitter并根据用户在前端输入的搜索词来流式传输数据(参见:twitter.tweetStream(api, params, values, track, bridgeArray, usernameArray, scoreData);
)。该模块在用户提交表单并执行/test
时执行,但即使在他们离开/test
后仍继续运行。它还与启动的任何新实例并行运行。
如果用户离开/test
路线,有没有办法告诉模块停止运行?个
// 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
});
});
为了清晰起见,添加了完整的代码:
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);
});