保护API令牌,使其成为我的index.js中的一个var

Securing API Token, is making it a var in my index.js possible?

本文关键字:var 一个 js index 令牌 API 我的 保护      更新时间:2023-09-26

所以我构建了一个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调用限制。

我想知道的是,我是否可以将其存储在我的index.js(我使用的是node.js模板)中作为var,然后在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'))
});

如果你只需要一个基本的模式w/a按钮来点击执行表单和提取电子邮件,我可以输入我的完整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("");
  });
});

我希望我能正确理解你的问题,这至少能让你朝着正确的方向前进。