node.js - 我需要我的 Discord Bot 处理外部网站的 GET 和 POST 请求

标签 node.js httprequest discord.js

我想让我的 Discord Bot 处理 GET 和 POST 请求,以使用仪表板控制我的 Bot 的某些功能。知道我的机器人与我 future 的仪表板托管在不同的服务器上。

有人谈到设置 RESTFul API,但经过一些研究后,我真的不知道该怎么做,也不知道它是否真的对我有帮助。我不能说其他方法是否可行。 我正在使用 node.js 和库 discord.js

最佳答案

您可以使用 express 为 GET 和 POST 做不同的事情
对网络服务器请求使用 POST,因为浏览器使用 GET。

注意:机器人会运行它来为网络服务器提供服务

var express = require('express');
var app = express();

var data = '{"example":{"online":true,"status":"200"}}';

app.get('/', function(req, res) {
    res.send('<html><head><script>window.location.href = \'https://google.com/\'</script></head><body>You shouldn\'t be here! <a href=\'https://google.com\'>Exit</a></body></html>');
    /** Redirect the browser to google with window.location.href 
     *  Change this to your site */
});

app.post('/', function(req, res) {
    /** You could add some auth code here but
     *  if your sending it all to the client there isn't much of a difference 
     *  because people could read it from the website. */
    res.type('json');
    res.json(data);
    /* Webserver --> Bot */
    res.end();
});

app.listen(8080);
console.log('API Online');

您将不得不使用 setInterval(() => {}, 15000); 来更新数据变量,您需要在客户端解析数据。
XMLHttpRequest 应该可以很好地工作

关于node.js - 我需要我的 Discord Bot 处理外部网站的 GET 和 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54260773/

相关文章:

discord - 当我们进行私信时如何在discord js中获取权限

node.js - 使用 node.js 登录 Firebase-Admin

node.js - 如何根据 mongodb (mongoose) 中填充的对象对元素进行排序

javascript - 使用 ejs 模板获取通过输入发布到同一页面的值

Python 请求包 : Handling xml response

javascript - 机器人命令未按预期工作

javascript - 创建解析角色层次结构失败并显示 'parent is undefined'

http - Web 服务器的连接数限制

python - 如何在 python 中使用 action=fetch 向 url 发送 HTTP POST 请求?

javascript - Discordjs 我的 react 收集器在我不希望它触发时触发