2017-02-21 18 views
0
const express = require('express'); 
const app = express(); 
var path = require('path'); 
app.get('/', function(req,res){ 
    res.sendFile(path.join(__dirname+'/login.html')); 
}); 
app.get('/views', function(req,res){ 
    res.sendFile(path.join(__dirname+'/views/index.html')); 
}); 


// Start the server 
const PORT = process.env.PORT || 8080; 
app.listen(PORT,() => { 
    console.log(`App listening on port ${PORT}`); 
    console.log('Press Ctrl+C to quit.'); 
}); 
// [END app] 

呈現login.html的作品沒有任何問題。我在我的視圖目錄中有index.html,並且很難渲染index.html。我知道應該使用app.use的地方,我已經嘗試過,但我不知道如何使這個工作正常,所以當我輸入localhost:8080/views,index.html應該彈出正確的一切工作在視圖文件夾中如何使用node.js快速呈現特定路徑下的某個特定路徑的html頁面

回答

0

您試圖實現的目標通常是使用express.static

的想法很簡單:

比方說,你想渲染你的目錄/views使/views/file1.txt發送path.join(__dirname+'/views/file1.txt')等。

你可以簡單地做

app.use('/views', express.static(path.join(__dirname, 'views'))); 

這將爲/views目錄中的所有文件的工作。


編輯

取決於您使用express的版本,你可能需要看看serve-staticexpress.static已被移除這是單機版。