2017-04-15 73 views
-2

我一直在尋找在這裏回答這個問題,但沒有任何幫助我。一些解決方案也給了我更多的錯誤。錯誤:無法找到與Node.JS和快遞模塊'html'

我想要做的是使用Node.JS路由到不同的頁面。在MEAN堆棧上學習了一點之後,我決定使用Node,Express和Angular(還沒有到那裏)。如果我試圖去任何不是'/'路線的地方,我會收到一個錯誤,說'無法找到模塊'html'。下面是有問題的代碼:

//dependencies 
var express = require('express'); 
var path = require('path'); 
var engines = require('consolidate') 

//variables 
var RUN_PORT = 8080; 
var VIDEO_IN_PORT = 45679; 
var CONTROL_OUT = 45678; 
var app = express(); 

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

//index.html routing 
app.get('/', function(req, res){ 
    res.render("index.html"); 
}); 

//robot_control.html routing 
app.get('/robot_control', function(req, res){ 
    res.render("robot_control.html"); 
}); 

//error.html routing 
app.get('/error', function(req, res){ 
    res.render("error.html"); 
}); 

//showing that the program is running on the RUN_PORT 
app.listen(RUN_PORT, function(){ 
    console.log("Running on port " + RUN_PORT); 
}); 

什麼我沒有看到嗎?一切似乎都很好。我在package.json中安裝的所有軟件都是Express。我能夠很好地進入索引頁面,但除此之外的任何內容都會導致該錯誤。

回答

1

問題是通過使用res.render('SOMEFILE.html')引起的,因爲你沒有告訴快遞如何使一個.html擴展名的文件。

既然你不使用任何模板(還),你可以這樣做:當你要開始使用模板

// Somewhere at the top: 
const path = require('path'); 
const VIEWS = path.join(__dirname, 'views'); 
... 

// In your route handlers: 
res.sendFile('index.html', { root : VIEWS }); 

,請看這裏:http://expressjs.com/en/guide/using-template-engines.html