是什么导致我的电报机器人出现此 404 错误
What is causing this 404 error on my telegram bot
const { Composer } = require('micro-bot');
const bot = new Composer;
const Telegram = require('telegraf/telegram')
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const telegram = new Telegram(process.env.BOT_TOKEN);
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/sms', (req, res) => {
console.log('sms received')
telegram.sendMessage(process.env.CHANNEL_ID, req.body.Body)
res.send(req.body.Body);
});
http.createServer(app).listen(3000, () => {
console.log(`Express server listening on port 3000`);
});
module.exports = bot;
每当我向机器人发送任何 http 请求时,我都会从 heroku 获取此应用程序日志 api:
at=info method=POST path="/sms" host=smsforwarder.herokuapp.com request_id=5dd30db1-aeb8-4f3f-8d56-0b0b576ef4e6 fwd="116.87.44.71" dyno=web.1 connect=0ms service=1ms status=404 bytes=101 protocol=https
当我尝试使用 env 端口时
const port = process.env.PORT || 3000
http.createServer(app).listen(port, () => {
console.log(`Express server listening on port ${port}`);
});
我收到一条错误消息,提示端口已被使用
2020-10-10T12:30:53.000000+00:00 app[api]: Build started by user ryanloh28@yahoo.com.sg
2020-10-10T12:31:07.143293+00:00 heroku[web.1]: State changed from crashed to starting
2020-10-10T12:31:06.978851+00:00 app[api]: Release v37 created by user ryanloh28@yahoo.com.sg
2020-10-10T12:31:06.978851+00:00 app[api]: Deploy 7e8e22a9 by user ryanloh28@yahoo.com.sg
2020-10-10T12:31:07.000000+00:00 app[api]: Build succeeded
2020-10-10T12:31:09.959000+00:00 heroku[web.1]: Starting process with command `micro-bot -p 4749`
2020-10-10T12:31:13.722882+00:00 heroku[web.1]: Process exited with status 1
2020-10-10T12:31:13.285259+00:00 app[web.1]: Express server listening on port 4749
2020-10-10T12:31:13.654218+00:00 app[web.1]: events.js:292
2020-10-10T12:31:13.654256+00:00 app[web.1]: throw er; // Unhandled 'error' event
2020-10-10T12:31:13.654257+00:00 app[web.1]: ^
2020-10-10T12:31:13.654257+00:00 app[web.1]:
2020-10-10T12:31:13.654258+00:00 app[web.1]: Error: listen EADDRINUSE: address already in use 0.0.0.0:4749
2020-10-10T12:31:13.654259+00:00 app[web.1]: at Server.setupListenHandle [as _listen2] (net.js:1313:16)
2020-10-10T12:31:13.654259+00:00 app[web.1]: at listenInCluster (net.js:1361:12)
2020-10-10T12:31:13.654260+00:00 app[web.1]: at doListen (net.js:1498:7)
2020-10-10T12:31:13.654261+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:85:21)
2020-10-10T12:31:13.654261+00:00 app[web.1]: Emitted 'error' event on Server instance at:
2020-10-10T12:31:13.654262+00:00 app[web.1]: at emitErrorNT (net.js:1340:8)
2020-10-10T12:31:13.654262+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:84:21) {
2020-10-10T12:31:13.654263+00:00 app[web.1]: code: 'EADDRINUSE',
2020-10-10T12:31:13.654263+00:00 app[web.1]: errno: 'EADDRINUSE',
2020-10-10T12:31:13.654264+00:00 app[web.1]: syscall: 'listen',
2020-10-10T12:31:13.654264+00:00 app[web.1]: address: '0.0.0.0',
2020-10-10T12:31:13.654264+00:00 app[web.1]: port: 4749
2020-10-10T12:31:13.654265+00:00 app[web.1]: }
2020-10-10T12:31:13.786291+00:00 heroku[web.1]: State changed from starting to crashed
我找到了解决问题的办法。我有一个让我的微型机器人在 env 端口上侦听的 procfile,这就是为什么当我让网络服务器同时侦听同一个端口时它抛出一个错误,说该端口已在使用中。
const { Composer } = require('micro-bot');
const bot = new Composer;
const Telegram = require('telegraf/telegram')
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const telegram = new Telegram(process.env.BOT_TOKEN);
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/sms', (req, res) => {
console.log('sms received')
telegram.sendMessage(process.env.CHANNEL_ID, req.body.Body)
res.send(req.body.Body);
});
http.createServer(app).listen(3000, () => {
console.log(`Express server listening on port 3000`);
});
module.exports = bot;
每当我向机器人发送任何 http 请求时,我都会从 heroku 获取此应用程序日志 api:
at=info method=POST path="/sms" host=smsforwarder.herokuapp.com request_id=5dd30db1-aeb8-4f3f-8d56-0b0b576ef4e6 fwd="116.87.44.71" dyno=web.1 connect=0ms service=1ms status=404 bytes=101 protocol=https
当我尝试使用 env 端口时
const port = process.env.PORT || 3000
http.createServer(app).listen(port, () => {
console.log(`Express server listening on port ${port}`);
});
我收到一条错误消息,提示端口已被使用
2020-10-10T12:30:53.000000+00:00 app[api]: Build started by user ryanloh28@yahoo.com.sg
2020-10-10T12:31:07.143293+00:00 heroku[web.1]: State changed from crashed to starting
2020-10-10T12:31:06.978851+00:00 app[api]: Release v37 created by user ryanloh28@yahoo.com.sg
2020-10-10T12:31:06.978851+00:00 app[api]: Deploy 7e8e22a9 by user ryanloh28@yahoo.com.sg
2020-10-10T12:31:07.000000+00:00 app[api]: Build succeeded
2020-10-10T12:31:09.959000+00:00 heroku[web.1]: Starting process with command `micro-bot -p 4749`
2020-10-10T12:31:13.722882+00:00 heroku[web.1]: Process exited with status 1
2020-10-10T12:31:13.285259+00:00 app[web.1]: Express server listening on port 4749
2020-10-10T12:31:13.654218+00:00 app[web.1]: events.js:292
2020-10-10T12:31:13.654256+00:00 app[web.1]: throw er; // Unhandled 'error' event
2020-10-10T12:31:13.654257+00:00 app[web.1]: ^
2020-10-10T12:31:13.654257+00:00 app[web.1]:
2020-10-10T12:31:13.654258+00:00 app[web.1]: Error: listen EADDRINUSE: address already in use 0.0.0.0:4749
2020-10-10T12:31:13.654259+00:00 app[web.1]: at Server.setupListenHandle [as _listen2] (net.js:1313:16)
2020-10-10T12:31:13.654259+00:00 app[web.1]: at listenInCluster (net.js:1361:12)
2020-10-10T12:31:13.654260+00:00 app[web.1]: at doListen (net.js:1498:7)
2020-10-10T12:31:13.654261+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:85:21)
2020-10-10T12:31:13.654261+00:00 app[web.1]: Emitted 'error' event on Server instance at:
2020-10-10T12:31:13.654262+00:00 app[web.1]: at emitErrorNT (net.js:1340:8)
2020-10-10T12:31:13.654262+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:84:21) {
2020-10-10T12:31:13.654263+00:00 app[web.1]: code: 'EADDRINUSE',
2020-10-10T12:31:13.654263+00:00 app[web.1]: errno: 'EADDRINUSE',
2020-10-10T12:31:13.654264+00:00 app[web.1]: syscall: 'listen',
2020-10-10T12:31:13.654264+00:00 app[web.1]: address: '0.0.0.0',
2020-10-10T12:31:13.654264+00:00 app[web.1]: port: 4749
2020-10-10T12:31:13.654265+00:00 app[web.1]: }
2020-10-10T12:31:13.786291+00:00 heroku[web.1]: State changed from starting to crashed
我找到了解决问题的办法。我有一个让我的微型机器人在 env 端口上侦听的 procfile,这就是为什么当我让网络服务器同时侦听同一个端口时它抛出一个错误,说该端口已在使用中。