所以我构建了一个 html 表单来与 Slack 交互。目前我的 js 代码如下所示。
$("#submitemail").click(function(){
$.post(
"https://openpgh.slack.com/services/hooks/incoming-webhook?token=MY_SECRET_TOKEN",
JSON.stringify({'text':'invite request from: '+$("#email").val(),'username':'Slack Inviter','icon_emoji':':raising_hand:'})
).success(function(){
$("#email").val("");
});
});
如果有人直接从我的 html 文件中复制此内容,他们只需运行控制台命令并更改 JSON,然后用大量废话轰炸我的 slack 组,直到达到 API 调用限制。
我想知道是否可以将其作为 var 存储在我的index.js(我使用的是node.js 模板)中,然后在html 中调用它。
非常感谢任何选项或建议,我对此很陌生。
我的结构是:
Slack App
|_node_modules
| |_express
|_public
| |_index.html
| |_node.svg (idk what this does)
|_.gitignore
|_app.json
|_index.js
|_package.json
|_procfile
|_README.md
我的index.js 的代码就是
var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'))
});
如果您希望它只是一个基本模式,带有一个按钮来单击以执行表单并提取电子邮件,我可以添加完整的 html。
最佳答案
免责声明:此代码未经测试
你基本上会做这样的事情:
index.js(评论解释我添加的内容):
var express = require('express');
// install request module
var request = require('request');
var app = express();
// make a new route that you can call from the client side
app.get('/getSlackData', function(req, res) {
//variable to hold your response from slack
var slackResponse;
//make the request to slack
var slackUrl = "https://openpgh.slack.com/services/hooks/incoming-webhook?token=MY_SECRET_TOKEN""
request(slackUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
slackReponse = response;
} else {
console.log(error);
});
return slackResponse;
});
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'))
});
所以我们添加了一个新的路由,它基本上是一个你可以从客户端调用的 API,它将返回你从 Slack 获得的 JSON 对象。您几乎可以保持客户端代码不变,只需更改您调用的路由即可:
$("#submitemail").click(function(){
$.post("/getSlackData",
JSON.stringify({'text':'invite request from:'+$("#email").val(),'username':'Slack Inviter','icon_emoji':':raising_hand:'})
).success(function(){
$("#email").val("");
});
});
我希望我正确理解了你的问题,这至少应该为你指明正确的方向。
关于javascript - 保护 API token ,是否可以将其作为我的 index.js 中的 var?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26310813/