使用Node的st模块提供index.html

bcs8qyzn  于 2023-06-05  发布在  Node.js
关注(0)|答案(3)|浏览(303)
  • 简而言之:当我访问/时,如何让stindex.html服务?*

我正在使用st module与Express(或Connect,无所谓)。下面是我的全部代码:

var st = require('st');
var path = require('path');
var connect = require('connect');

var app = connect();

app.use(st({
  url: '/',
  path: path.resolve(__dirname, 'public'),
  index: 'index.html', // !!! PROBLEM LINE !!!
  cache: false,
  passthrough: true
}));

app.use(function(req, res) {
  res.end('This is not served from the st module.');
});

app.listen(8000, function() { console.log('App started.'); });

当我访问localhost:8000/index.html时,我看到了这个文件。当我访问localhost:8000/时,我看不到索引HTML。
我尝试了以下index选项,但没有成功:

  • index: false
  • index: true
  • index: 'index.html'
  • index: 'public/index.html'
  • index: path.resolve(__dirname, 'public/index.html')

当我访问/时,如何让stindex.html提供服务?

pxyaymoc

pxyaymoc1#

我实现了一个重定向,类似于:

routes['/'] = function(req, res) {
    res.redirect('/index.html');
};

而且很有效!

mbyulnm0

mbyulnm02#

index.html移动到public目录。因为st处理请求如下:

if (u.indexOf(this.url) !== 0) return false

这意味着您当前访问的URL必须是app.use()设置的选项url的子URL。
在本例中,您将'/public'Map到'/'。因此,您不能使用public/index.html,除非您在原始public文件夹下创建另一个名为public的文件夹,并将index.html放入其中。
您也不能使用../index.html,因为st不允许这样做(您正在访问一个取消Map文件!),你会得到一个403错误。
如果仍然失败,请尝试其他Web浏览器。有一次我在Chrome上试了一下,没问题,但在Firefox上失败了。这可能是该高速缓存选项引起的。

aurhwmvo

aurhwmvo3#

这是:

st({ 
    path: __dirname + "/web", 
    url:"/", 
    index: "/index.html" 
});

对我很有效但我得先清除浏览器缓存。

相关问题