The*_*ode -1 javascript node.js express mean-stack meanjs
我一直在查看这里对这个问题的答复,但没有任何帮助我。有些解决方案也给我带来了更多错误。
我想做的是使用 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);
});
Run Code Online (Sandbox Code Playgroud)
我在这里没有看到什么?一切看起来都很好。我在 package.json 中安装的只是 Express。我能够很好地访问索引页,但除此之外的任何其他内容都会导致该错误。
该问题是由使用引起的res.render('SOMEFILE.html'),因为您没有告诉 Express 如何渲染带有.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 });
Run Code Online (Sandbox Code Playgroud)
当您想开始使用模板时,请查看此处: http: //expressjs.com/en/guide/using-template-engines.html
| 归档时间: |
|
| 查看次数: |
5252 次 |
| 最近记录: |