javascript - 保护 API token ,是否可以将其作为我的 index.js 中的 var?

标签 javascript node.js

所以我构建了一个 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 存储在我的i​​ndex.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/

相关文章:

javascript - 从 node.js 开始,出现错误 Uncaught ReferenceError : module is not defined, 和 Uncaught ReferenceError: require is not Defined

javascript - 将 onselectionchange 添加到 vue

node.js - 在 Express 3.x 中使用布局

javascript - delete object[element] 不会删除,而是将其设置为 null ....如何删除它而不是使其为 null?

javascript - 向从 Mage::getBlockSingleton 返回的对象添加 html 属性

php - 如何使用 PHP mcrypt 加密并使用 Node Crypto 解密

javascript - 查看函数的定义位置

JavaScript 变量作用域 - 正确使用

javascript - Ionic 带有地理位置警报 : 'mySecondApp.app/www/index.html' Would like to use your current location

javascript - 如何处理 Mustache 模板中的 IF 语句?