[英]Problems deploying to heroku
So this is my basic Twitter bot which uses Node to look for search terms from @ references and then find animated gifs associated with that: 所以这是我的基本Twitter机器人,它使用Node从@引用中查找搜索词,然后查找与此相关的动画gif:
//Dependencies
const Twitter = require('twitter');
const http = require('http');
const config = require('./config.js');
const giphyApiKey = (process.env.apiKey || config.giphy.apiKey);
let searchString = "";
let giphyQueryUrl;
//Initialize twitter client
const client = new Twitter({
consumer_key: (process.env.consumer_key || config.twitter.consumer_key),
consumer_secret: (process.env.consumer_secret || config.twitter.consumer_secret),
access_token_key: (process.env.access_token_key || config.twitter.access_token_key),
access_token_secret: (process.env.access_token_secret || config.twitter.access_token_secret)
});
process.on('unhandledRejection', (reason,promise) => {
console.log('Unhandled Rejection at:', promise, 'reason:', reason);
})
//This stream checks for statuses containing '@[USERNAME]' references, strips out the relevant seach data and feeds
//that search data to the queryGiphy function
client.stream('statuses/filter', {track: '@[USERNAME]'}, (stream) => {
stream.on('data', (tweet) => {
searchString =
tweet.text.substring(tweet.text.indexOf("@[USERNAME]")+"@[USERNAME]".length + 1,tweet.text.length);
giphyQueryUrl = "http://api.giphy.com/v1/gifs/search?q="+searchString+"&api_key="+ giphyApiKey +"&limit=5&rating=g";
let replyString = '@' + tweet.user.screen_name + ' ';
let giphyUrl = queryGiphy(replyString,giphyQueryUrl);
})
})
//This function will query the Giphy API using the node http module and when it finds a match, posts that to twitter
//using the gifUrlToPost function
function queryGiphy(replyString,queryUrl) {
http.get(queryUrl, res => {
res.setEncoding("utf8");
let body = '';
res.on("data", data => {
body += data;
});
res.on("end", () => {
body = JSON.parse(body);
if(postToTwitter(replyString + body.data[0].url)) {
return true;
} else {
return false;
}
});
res.on("error", error => {
console.log("Error: " + error);
})
});
}
//This simply posts the url of the gif passed to it
function postToTwitter(body) {
client.post('statuses/update', {status: body})
.then(tweet => {
return true;
})
.catch(error => {
throw error;
});
}
My problem is that when I deploy this to heroku, I keep getting a timeout error that there is no place to connect $PORT to for heroku to dynamically assign a port. 我的问题是,当我将其部署到heroku时,我总是收到超时错误,没有地方可以将$ PORT连接到heroku来动态分配端口。 Now, I understand the nature of this, I've deployed small web servers to heroku before and understood how to set up environmental variables so that it can dynamically assign a port number for setting up a stream with Twitter and querying Giphy. 现在,我了解了它的本质,我之前已经在heroku上部署了小型Web服务器,并且了解了如何设置环境变量,以便它可以动态分配端口号,以使用Twitter设置流并查询Giphy。 But where do I do it in my code? 但是我在代码中的哪儿做呢? I don't understand, in this case, where to do it? 在这种情况下,我不知道该在哪里做?
do you have app.js file or the bin file if you do have then 您是否有app.js文件或bin文件(如果有)
app.set('port',(process.env.PORT||5000));
app.listen(app.get('port'),function(){
})
or you need to create a Procfile in your root and push the code to heroku git 或者您需要在根目录中创建一个Procfile并将代码推送到heroku git
you may read this article for futher clarification https://devcenter.heroku.com/articles/procfile 您可以阅读本文以进一步了解https://devcenter.heroku.com/articles/procfile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.