简体   繁体   中英

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

I have the following code in a nodejs app, which routes / and asks for Express authorization in order to login and access .

// 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');
}

I tried changing this to a different path but for some reason every path I type in my app url http://app.com:8000/ is getting routed through Express.

My goal is to have the Express authentication enabled only for 2 specific paths while allowing /path2 only for a specific case (if there is ?id= in the url), for which I believe should use some regex.

/path1

/path2

How can I accomplish this and have both path1 and path2 routed through Express while allowing /path2/myfile.php?id= to be visited ?

Rather then "avoiding" express for certain routes, why not get req.params to get the param ( http://expressjs.com/en/api.html ), and see if id is in there ie

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

if you really want to avoid using node for query params you can set this up using routing via nginx or apache before node is even hit. Also google Express router, there are better ways to structure routing (in my biased opinion)

I also think you should step back one abstraction level and deal with this on the webserver level.

If you are using nginx, I would check the 'location' directive, and instead of bothering with regexes I would have a specific path for php-related resources. A very simple configuration file might look like:

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;
}
}

Here is a link on how to make nginx work together with Node.js https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

Here is how to setup nginx to serve php http://www.sitepoint.com/setting-up-php-behind-nginx-with-fastcgi/

And here is the documentation on 'location' directive http://nginx.org/en/docs/http/ngx_http_core_module.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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