![](/img/trans.png)
[英]Problems with public directory when deploying Node.js app with Heroku
[英]Problems deploying to heroku
所以這是我的基本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;
});
}
我的問題是,當我將其部署到heroku時,我總是收到超時錯誤,沒有地方可以將$ PORT連接到heroku來動態分配端口。 現在,我了解了它的本質,我之前已經在heroku上部署了小型Web服務器,並且了解了如何設置環境變量,以便它可以動態分配端口號,以使用Twitter設置流並查詢Giphy。 但是我在代碼中的哪兒做呢? 在這種情況下,我不知道該在哪里做?
您是否有app.js文件或bin文件(如果有)
app.set('port',(process.env.PORT||5000));
app.listen(app.get('port'),function(){
})
或者您需要在根目錄中創建一個Procfile並將代碼推送到heroku git
您可以閱讀本文以進一步了解https://devcenter.heroku.com/articles/procfile
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.