simplest way to have Express serve a default page?

后端 未结 4 568
误落风尘
误落风尘 2021-01-01 12:16

I\'m using this to have Node.js/Express setup as a rudimentary web server - it just serves a set of static pages without any other processing. I\'d like it to always serve /

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 12:55

    It's weird that no one mentioned redirection.

    app.get('/', function(req, res){
        res.redirect('/default.html');
    });
    

    This -of course- assumes that you have your 'default.html' at the public path. You can set your public path by using:

    app.use(express.static(relative_path_to_project_root));
    

提交回复
热议问题