提问者:小点点

使用Express.Static后get内的代码未运行


这是我的nodejs express设置:

var express = require('express');
var app = express();

app.use(express.static('public'));

app.get('*', function (req, res) {
  res.sendfile('public/index.html')
});

app.listen(process.env.PORT || 3000, function () {
  console.log('Example app listening on port 3000!');
});


module.exports = app;

我发现当它使用:

app.use(express.static(“public”));

get中不运行任何内容:

app.get(“*”,function(req,res){//nothing res.sendfile(“public/index.html”)});

PS:我想在get内部重定向(HTTP->https)。


共1个答案

匿名用户

如果您想要将所有http请求重定向到https,那么插入一个app.use()中间件作为第一个具有重定向逻辑的请求处理程序到https。Express按照定义请求处理程序的顺序处理请求处理程序,并且您希望在Express.static()中间件之前首先处理该中间件。

当然,您需要将所有其他请求处理程序放在https服务器上(而不是http服务器上),这样https服务器就可以在重定向后处理您的URL(这是您的代码没有显示的)。