繁体   English   中英

如何在我的应用程序中为特定路径禁用NodeJS Express,并允许其中之一访问特定的正则表达式?

[英]How can I disable NodeJS Express for specific paths in my app and allow access to a specific regex in one of them?

我在nodejs应用程序中有以下代码,该应用程序路由/并请求Express授权以登录和访问。

// Routes

require('./routes/check')(app);
require('./routes/tag')(app);
require('./routes/ping')(app);

// route list
app.get('/', function(req, res) {
  var routes = [];
  for (var verb in app.routes) {
    app.routes[verb].forEach(function(route) {
      routes.push({method: verb.toUpperCase() , path: app.route + route.path});
    });
  }
  res.json(routes);
});

if (!module.parent) {
  app.listen(3000);
  console.log('Express started on port 3000');
}

我尝试将其更改为其他路径,但是由于某种原因,我在应用程序网址http://app.com:8000/中键入的每个路径都已通过Express路由。

我的目标是仅对2条特定路径启用Express身份验证,而仅在特定情况下(如果url中有?id =)允许/ path2启用,我认为应该使用一些正则表达式。

/路径1

/路径2

我如何才能做到这一点,同时允许路径1和路径2通过Express路由,同时允许访问/path2/myfile.php?id=?

而不是“避免”表达某些路线,为什么不获取req.params以获得参数( http://expressjs.com/en/api.html ),并查看id是否在其中,即

var id = req.params.id
if(!id)
 //require authentication

如果您真的想避免使用节点作为查询参数,则可以在节点未命中之前通过nginx或apache使用路由来进行设置。 也是谷歌快递路由器,有更好的方法来构造路由(以我的偏见)

我还认为您应该退后一个抽象级别,并在Web服务器级别进行处理。

如果您使用的是nginx,我将检查“ location”指令,而不是烦恼正则表达式,而会使用与php相关的资源的特定路径。 一个非常简单的配置文件可能如下所示:

server {
listen 80;

server_name example.com;

location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
location /php\.php$ {
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass 127.0.0.1:8000;
  fastcgi_index index.php;
  include fastcgi_params;
}
}

这是有关如何使Nginx与Node.js一起工作的链接https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on -ubuntu-14-04

这是如何设置Nginx来服务php http://www.sitepoint.com/setting-up-php-behind-nginx-with-fastcgi/

这是有关“位置”指令的文档http://nginx.org/en/docs/http/ngx_http_core_module.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM