Http请求不会路由到Https NodeJs

问题描述:

我在 Google App Engine 上有我的服务器,我使用npm模块 yes-https ,下面是我写的代码,用于限制对Https的请求。

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

此前的代码工作正常,并且我的所有请求都是 Https OR Http 都被路由到 Https 。但现在我不知道为什么来自Http的请求没有被路由到Https。

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.

你好,其实这个插件工作得很好。我所做的错误是在设置之后,在中间件上放置 yes-https > / public 目录,但正确的方法是在设置 / public 目录之前放置它。

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

因为 Https 没有执行,但现在一切都按预期工作。

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