将nodejs bot部署到now.sh

时间:2019-03-27 16:12:06

标签: javascript node.js github web-deployment zeit-now

我在将nodejs机器人部署到now.sh服务时遇到问题,这是我的配置:

index.js

var http = require("http");
var Twit = require("twit");

const tweets = [
  {
    text: "1",
    img: `...`
  },
  {
    text: "2",
    img: `...`
  },
  {
    text: "3",
    img: `...`
  }
];

var T = new Twit({
  consumer_key: "...",
  consumer_secret: "...",
  access_token: "...",
  access_token_secret: "...",
  timeout_ms: 60 * 1000,
  strictSSL: true
});

function postImage() {
  console.log("running");
  const randomNum = Math.floor(Math.random() * (tweets.length - 0));
  T.post(
    "media/upload",
    {
      media_data: tweets[randomNum].img
    },
    ...        }
      });
    }
  );
}

http
  .createServer(function(req, res) {
    if (req.url != "/favicon.ico") {
      postImage();
      setInterval(postImage, 10000);
    }

    res.end();
  })
  .listen();

now.json

{
  "name": "twitter-bot",
  "builds": [
    {
      "src": "./index.js",
      "use": "@now/node-server"
    }
  ]
}

在本地测试中,要执行功能,我必须打开localhost:port,然后重新加载页面以执行功能。 所以,我不知道到底是什么问题?

0 个答案:

没有答案