[英]Is it possible to define different locations in your NPM package for browser and for server (NodeJS)?
[英]Is it possible to launch js files with different permissions using nodejs/npm?
我想在具有不同权限的 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
}
有谁知道如何做这样的事情?
根据此处问题的片段,您可能会覆盖需要 function 来检查文件名,如下所示:
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);
};
然后在您已经需要主文件中的配置后执行此覆盖,这样您就不会意外阻止自己
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.