I am not using any any template engine. I want to redirect everything to my static file /public/desktop.html

app.use(express.static(__dirname + '/public'));

function route(req, res, next) {
    res.sendfile(__dirname + '/public/desktop.html');
    myURL = url.parse(req.url).pathname;
}

It works well if I use this and access 'localhost:8080/anypath on the url

But if I try 'localhost:8080/' I get nothing:

app.get('*', route); 

And I can't access anything if I use any of those:

app.get('/', route);
app.get('/*', route); 
有帮助吗?

解决方案

app.use(express.static(__dirname + '/public')) is mounting a static file handler that is turning the '/' into '/index.html' and sending a 404 because it can't find index.html

If you change the ordering around:

function route(req, res, next) {
  if(req.url!='/'){
    return next();
  }
  res.sendfile(__dirname + '/public/desktop.html');
}

app.get('/', route);
app.use(express.static(__dirname + '/public'));

It might work?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top