簡體   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