simplest way to have Express serve a default page?

后端 未结 4 537
误落风尘
误落风尘 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:58

    I looked briefly for the best practices for serving a public folder and specifying the default page to serve. After reviewing the 'Express middleware' documentation, my solution looks like the following,

    var express = require('express');
    var app = express();
    
    var options = {
      index: "coming-soon.html"
    };
    
    app.use('/', express.static('app', options));
    
    var server = app.listen(8081, function () {
      var host = server.address().address;
      var port = server.address().port;
    
      console.log('my app is listening at http://%s:%s', host, port);
    });
    

提交回复
热议问题