Http Requests not getting routed to Https NodeJs

2019-06-08 09:43发布

I have my Server on Google App Engine and I am using npm module yes-https, below is the code I am writing in order to restrict the requests to Https.

app.use(yes({
   maxAge: 86400,            // defaults `86400`
   includeSubdomains: true,  // defaults `true`
   preload: true             // defaults `true`           
}));

Previously this code was working fine and all my requests whether Https OR Http all were getting routed to Https. But now I don't know why requests coming to Http is not getting routed to Https.

Can anyone please tell me why is this happening.

1条回答
做个烂人
2楼-- · 2019-06-08 10:37

Hello actually this plugin is working perfectly fine. The mistake I was doing was I was putting yes-https on the middleware after the setting the /public directory, but the right way was to put it before setting /public directory.

Right Code

app.use(yes({
  maxAge: 86400,            // defaults `86400`
  includeSubdomains: true,  // defaults `true`
  preload: true             // defaults `true`           
}));
app.use(express.static(__dirname + '/public'));

Wrong Code

app.use(express.static(__dirname + '/public'));
app.use(yes({
  maxAge: 86400,            // defaults `86400`
  includeSubdomains: true,  // defaults `true`
  preload: true             // defaults `true`           
}));

Because of which Https was not enforcing but now everything is working as expected.

查看更多
登录 后发表回答