javascript - 是否可以使用 nodejs/npm 启动具有不同权限的 js 文件?
问题描述
我想在具有不同权限的 js 文件中启动 js 文件。像这样:
main.js
(开始)
config = JSON.parse(require("./config.json")) // <- should be possible
console.log(config.authkey) // <- should be possible
require("./randomJSFile.js").run()
randomJSFile.js
(将由 main.js 执行)
exports.run = () => {
let config = JSON.parse(require("./config.json") // <--- this should not be possible, only the main.js file should have access to the config.json file
console.log(config.authkey) // should not be possible
}
有谁知道如何做这样的事情?
解决方案
根据此处问题的片段,您可能会覆盖 require 函数来检查文件名,如下所示:
const Module = require('module');
const originalRequire = Module.prototype.require;
Module.prototype.require = function() {
if (!arguments.length || typeof arguments[0] !== 'string') return {};
if (arguments[0].includes('config.json')) return {};
return originalRequire.apply(this, arguments);
};
然后在您已经需要主文件中的配置后执行此覆盖,这样您就不会意外阻止自己
推荐阅读
- fonts - 如何填充轮廓字体
- c++ - gethostbyname() 和 getlogin_r() 等函数在远程调试时停止运行时执行
- python - 将python代码转换为Go进行json编码
- python - Python,openpyxl:下载多个 excel 表
- sql - 具有聚合函数和 group by 子句的 SQL 查询
- javascript - 单击输入字段时,ANTD 下拉菜单不应关闭
- javascript - 如果键存在,则从对象中获取与整数键关联的值 (JavaScript)
- git - 克隆 repo,只有几个可用的分支
- jestjs - 无法使用 jest-puppeteer 运行 Jest Snapshot 测试
- unity3d - Unity 3D游戏对象不移动