Javascript 使用Express限制对Node.js的访问

Javascript 使用Express限制对Node.js的访问,javascript,node.js,express,server,restriction,Javascript,Node.js,Express,Server,Restriction,我确实在服务器中有一个正在运行的node.js脚本。 我想要的是,它不能直接从浏览器访问,而且我希望只有某些域/IP-s可以调用它! 可能吗 不确定如何区分从浏览器访问内容与从其他软件访问内容,但限制对某些域/IP的访问应该是可行的。以下(非生产)代码限制对localhost环回的访问,可以作为起点: function securityCheck( req, response, next) { var callerIP = req.connection.remoteAddress;

我确实在服务器中有一个正在运行的node.js脚本。 我想要的是,它不能直接从浏览器访问,而且我希望只有某些域/IP-s可以调用它!
可能吗

不确定如何区分从浏览器访问内容与从其他软件访问内容,但限制对某些域/IP的访问应该是可行的。以下(非生产)代码限制对localhost环回的访问,可以作为起点:

function securityCheck( req, response, next)
{    var callerIP = req.connection.remoteAddress;
     (callerIP == "::ffff:127.0.0.1" || callerIP == "::1") ? next() : reply404( response);
}

function reply404( response)
{   response.writeHead(404, {"Content-Type": "text/html"});
    response.statusMessage = "Not Found";
    console.log("reply404: statusCode: " + response.StatusCode);
    response.end('<span style="font-weight: bold; font-size:200%;">ERROR 404 &ndash; Not Found<\/span>');
}
var app = express();
app.use(securityCheck);  // restrict access
... // continue with app routing
函数安全检查(请求、响应、下一步)
{var callerIP=req.connection.remoteAddress;
(callerIP==”::ffff:127.0.0.1“| | callerIP==”::1“?next():reply404(响应);
}
功能应答404(应答)
{response.writeHead(404,{“内容类型”:“text/html”});
response.statusMessage=“未找到”;
console.log(“reply404:statusCode:+response.statusCode”);
response.end('ERROR 404&ndash;Not Found');
}
var-app=express();
应用程序使用(securityCheck);//限制访问
... // 继续应用程序路由

另请参阅更多详细信息,以便了解和的答案,因为据我所知,Express无法像Nginx和Apache那样单独执行条件块。所以我现在没有主意了。