【问题标题】:How to white list sub domains in Node js如何在 Node js 中将子域列入白名单
【发布时间】:2023-01-11 13:54:35
【问题描述】:
我正在使用下面的正则表达式将域列入白名单,但是下面的代码 xxx.sampledomain.com 不起作用
let url = req.headers.origin.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "").split('/')[0]
let client = CLIENTS.filter(client => client.websiteAddress.replace('www.', '') === url)[0];
请指导
【问题讨论】:
标签:
javascript
node.js
regex
【解决方案1】:
我正在添加一个代码。它会检查。是否允许该子域
// List of allowed subdomains
const allowedSubdomains = ["subdomain1", "subdomain2", "subdomain3"];
const subdomainRegex = new RegExp(`^(?:https?://)?(?:www.)?(${allowedSubdomains.join("|")}).`);
// Function to check if a subdomain is allowed
function isAllowedSubdomain(subdomain) {
return subdomainRegex.test(subdomain);
}
例如:
let subdomain = req.headers.origin.match(subdomainRegex)[1];
// Extract the subdomain from the origin
if (isAllowedSubdomain(subdomain)) {
console.log("Allow this subdomain")
} else {
console.log("This subdomain is not allowed")
}
如果你有任何问题,你可以问