2016-06-12 35 views
0

我在我的Express服務器上使用Wildcard Subdomains,因此當用戶轉到name.example.com或從該子域請求某些API時,它一切正常。 (導航到name.example.com/api工作正常)Express,Wildcard-Subdomains和sendFile()

但是,實際導航到name.example.com需要提供index.html文件;我使用下面的代碼作爲一個catchall,但是鏈接到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 

如果有更好的包,我應該使用,請讓我知道!

謝謝!

回答

1

這工作:

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