[英]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 . 我在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');
}
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. 我尝试将其更改为其他路径,但是由于某种原因,我在应用程序网址http://app.com:8000/中键入的每个路径都已通过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. 我的目标是仅对2条特定路径启用Express身份验证,而仅在特定情况下(如果url中有?id =)允许/ path2启用,我认为应该使用一些正则表达式。
/path1 /路径1
/path2 /路径2
How can I accomplish this and have both path1 and path2 routed through Express while allowing /path2/myfile.php?id= to be visited ? 我如何才能做到这一点,同时允许路径1和路径2通过Express路由,同时允许访问/path2/myfile.php?id=?
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 而不是“避免”表达某些路线,为什么不获取req.params以获得参数( http://expressjs.com/en/api.html ),并查看id是否在其中,即
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. 如果您真的想避免使用节点作为查询参数,则可以在节点未命中之前通过nginx或apache使用路由来进行设置。 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. 我还认为您应该退后一个抽象级别,并在Web服务器级别进行处理。
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. 如果您使用的是nginx,我将检查“ location”指令,而不是烦恼正则表达式,而会使用与php相关的资源的特定路径。 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 这是有关如何使Nginx与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/ 这是如何设置Nginx来服务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 这是有关“位置”指令的文档http://nginx.org/en/docs/http/ngx_http_core_module.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.