【问题标题】:Express, Wildcard-Subdomains, and sendFile()Express、通配符子域和 sendFile()
【发布时间】:2016-10-12 16:52:28
【问题描述】:

我在我的 Express 服务器上使用 Wildcard Subdomains,因此当用户转到 name.example.com 或从该子域的 API 请求某些内容时,一切正常。 (导航到name.example.com/api 工作正常)

但是,实际上导航到name.example.com 需要提供index.html 文件;我使用下面的代码作为总括,但是任何链接到 HTML 文件内部的文件(如样式表或 JS 文件)都使用index.html 的内容提供服务。

// routes/routefile.js
router.get('/_sub/:name/*', (req, res) => {
  res.sendFile(path.resolve(__dirname, '..', 'public', 'index.html'));
});

我的文件结构:

Project/
|_ routes/
|_ public/
|_ server.js

如果有更好的包我应该使用,请告诉我!

谢谢!

【问题讨论】:

    标签: javascript node.js express routing wildcard-subdomain


    【解决方案1】:

    这行得通:

    app.use('/_sub/:name/', express.static(path.join(__dirname + '/public/')));

    【讨论】:

      猜你喜欢
      • 2013-09-20
      • 2014-06-12
      • 2020-08-23
      • 2013-08-18
      • 2011-06-05
      • 2010-09-09
      • 2019-03-18
      • 2011-10-03
      • 2017-06-22
      相关资源
      最近更新 更多