im using nodejs and im trying to serve only html files (no jade, ejs ... engines).
即时通讯使用nodejs和我试图只提供html文件(没有玉,ejs ...引擎)。
heres my entry point (index.js) code:
继承我的入口点(index.js)代码:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
app.get('*', function(req, res){
res.render('index.html');
});
app.listen(app.get('port'), function() {
});
This is doing just fine when i hit the url "localhost:5000/", but when i try something like "localhost:5000/whatever" i got the following message: Error: Cannot find module 'html'
当我点击网址“localhost:5000 /”时,这样做很好,但是当我尝试类似“localhost:5000 / whatever”之类的内容时,我收到以下消息:错误:无法找到模块'html'
im new to nodejs, but i want all routes to render the index.html
file. How can i do that ???
我是nodejs的新手,但我希望所有路由都呈现index.html文件。我怎样才能做到这一点 ???
Thank you.
谢谢。
3 个解决方案
#1
22
You need to specify your view folder and parse the engine to HTML.
您需要指定视图文件夹并将引擎解析为HTML。
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.get('*', function(req, res){
res.render('index.html');
});
app.listen(app.get('port'), function() {
});
#2
5
One would use render only when using some rendering engines such as jade or ejs, if you plan to use plain HTML, place it in the public folder or serve it as a static file.
只有在使用某些渲染引擎(例如jade或ejs)时才会使用渲染,如果您打算使用纯HTML,将其放在公用文件夹中或将其作为静态文件提供。
res.sendFile('index2.html', {root : __dirname + '/views'});
#3
0
First of all you need to install ejs engine. For that you can use the following code
首先,您需要安装ejs引擎。为此,您可以使用以下代码
npm install ejs
After that you need to add app engine and set the view directory.
之后,您需要添加应用引擎并设置视图目录。
The changed code is given below,
更改的代码如下,
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.listen(app.get('port'), function() {
});
#1
22
You need to specify your view folder and parse the engine to HTML.
您需要指定视图文件夹并将引擎解析为HTML。
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.get('*', function(req, res){
res.render('index.html');
});
app.listen(app.get('port'), function() {
});
#2
5
One would use render only when using some rendering engines such as jade or ejs, if you plan to use plain HTML, place it in the public folder or serve it as a static file.
只有在使用某些渲染引擎(例如jade或ejs)时才会使用渲染,如果您打算使用纯HTML,将其放在公用文件夹中或将其作为静态文件提供。
res.sendFile('index2.html', {root : __dirname + '/views'});
#3
0
First of all you need to install ejs engine. For that you can use the following code
首先,您需要安装ejs引擎。为此,您可以使用以下代码
npm install ejs
After that you need to add app engine and set the view directory.
之后,您需要添加应用引擎并设置视图目录。
The changed code is given below,
更改的代码如下,
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.listen(app.get('port'), function() {
});