Express中静态html页面的多个路由

时间:2017-08-03 21:43:32

标签: node.js express

我正在尝试从Express提供2个静态HTML页面,但是当index.html正确提供时,当我尝试访问/ about路径时出现错误:

  

错误:ENOENT:没有这样的文件或目录,stat   ' /var/www/html/myapp/about.html'       在错误(本机)

var express = require('express'),
  app = express(),
  http = require('http'),
  httpServer = http.Server(app);

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

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

app.get('/about', function(req, res) {
  res.sendfile(__dirname + '/about.html');
});


app.listen(3000);

我可以更新' /about.html'到' /html_files/about.html'然后它起作用但是虽然这解决了这个问题,但我无法理解为什么它不能正常工作。

1 个答案:

答案 0 :(得分:0)

看起来不错。

app.get('/'...将被忽略,因为服务器已找到与此请求匹配的静态index.html

app.get('/about'...失败,因为您提交了错误的网址并试图发送。如果您要求/about.html,它将被正确的静态中间件发送。