渲染基本的 HTML 视图?
我有一个基本的 Node.js 应用程序,我正在尝试使用 Express 框架来启动它。我有一个 views
文件夹,其中有一个 index.html
文件。但加载网页时出现以下错误:
Error: Cannot find module 'html'
下面是我的代码。
var express = require('express');
var app = express.createServer();
app.use(express.staticProvider(__dirname + '/public'));
app.get('/', function(req, res) {
res.render('index.html');
});
app.listen(8080, '127.0.0.1')
我在这里缺少什么?
I have a basic Node.js app that I am trying to get off the ground using the Express framework. I have a views
folder where I have an index.html
file. But I receive the following error when loading the web page:
Error: Cannot find module 'html'
Below is my code.
var express = require('express');
var app = express.createServer();
app.use(express.staticProvider(__dirname + '/public'));
app.get('/', function(req, res) {
res.render('index.html');
});
app.listen(8080, '127.0.0.1')
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
你可以让jade包含一个纯HTML页面:
在views/index.jade中
,在views/plain.html中
,app.js仍然可以只渲染jade:
You can have jade include a plain HTML page:
in views/index.jade
in views/plain.html
and app.js can still just render jade:
其中许多答案都已过时。
使用express 3.0.0和3.1.0,可以执行以下操作:
请参阅下面的注释,了解express 3.4+的替代语法和注意事项:
然后您可以执行类似的操作:
这假设您的视图位于
views
子文件夹中,并且您已经安装了ejs
节点模块。如果没有,请在 Node 控制台上运行以下命令:Many of these answers are out of date.
Using express 3.0.0 and 3.1.0, the following works:
See the comments below for alternative syntax and caveats for express 3.4+:
Then you can do something like:
This assumes you have your views in the
views
subfolder, and that you have installed theejs
node module. If not, run the following on a Node console:来自 Express.js 指南:查看渲染
因此,要么创建自己的简单渲染器,要么只使用jade:
更多关于
app.register
。From the Express.js Guide: View Rendering
So either you create your own simple renderer or you just use jade:
More about
app.register
.您还可以读取 HTML 文件并将其发送:
You could also read the HTML file and send it:
试试这个。它对我有用。
try this. it works for me.
如果您使用的是 express@~3.0.0,请将示例中的以下行更改
为类似这样的内容:
我按照 express api 页面 它的工作方式就像魅力一样。通过该设置,您无需编写额外的代码,因此可以轻松用于微生产或测试。
完整代码如下:
If you're using express@~3.0.0 change the line below from your example:
to something like this:
I made it as described on express api page and it works like charm. With that setup you don't have to write additional code so it becomes easy enough to use for your micro production or testing.
Full code listed below:
我在
express 3.X
和node 0.6.16
中也遇到了同样的问题。上述解决方案不适用于最新版本express 3.x
。他们删除了app.register
方法并添加了app.engine
方法。如果您尝试上述解决方案,您可能会遇到以下错误。摆脱错误消息。将以下行添加到您的
app.configure 函数
注意:您必须安装
ejs
模板引擎示例:
注意: 最简单的解决方案是使用ejs 模板作为视图引擎。在那里,您可以在 *.ejs 视图文件中编写原始 HTML。
I also faced the same issue in
express 3.X
andnode 0.6.16
. The above given solution will not work for latest versionexpress 3.x
. They removed theapp.register
method and addedapp.engine
method. If you tried the above solution you may end up with the following error.To get rid of the error message. Add the following line to your
app.configure function
Note: you have to install
ejs
template engineExample:
Note: The simplest solution is to use ejs template as view engine. There you can write raw HTML in *.ejs view files.
文件夹结构:
server.js
index.html
输出:
hello world
folder structure:
server.js
index.html
output:
hello world
如果你想渲染 HTML 文件,你可以使用
sendFile()
方法,而不使用任何模板引擎。我在 htmlfile 中有一个 HTML 文件,所以我使用路径模块来渲染 index.html path 是节点中的默认模块。如果您的文件存在于刚刚
在
app.get()
中使用的根文件夹中,它将起作用If you want to render HTML file you can use
sendFile()
method without using any template engineI have an HTML file inside htmlfile so I used path module to render index.html path is default module in node. if your file is present in root folder just used
inside
app.get()
it will work如果您不必使用views目录,只需将html文件移动到下面的public目录即可。
然后,将此行添加到 app.configure 而不是“/views”中。
If you don't have to use the views directory, Simply move html files to the public directory below.
and then, add this line into app.configure instead of '/views'.
对于我的项目,我创建了以下结构:
此代码为
/
请求提供 index.html,为/css/reset.css
请求提供 reset.css。足够简单,最好的部分是它会自动添加缓存标头。For my project I have created this structure:
This code serves index.html for
/
requests, and reset.css for/css/reset.css
requests. Simple enough, and the best part is that it automatically adds cache headers.要在节点中渲染 Html 页面,请尝试以下操作,
您需要通过
npm
安装ejs
模块,如下所示:To render Html page in node try the following,
You need to install
ejs
module throughnpm
like:使用 Express 4.0.0,您唯一要做的就是注释掉 app.js 中的 2 行:
然后将静态文件放入 /public 目录中。示例:/public/index.html
With Express 4.0.0, the only thing you have to do is comment out 2 lines in app.js:
And then drop your static file into the /public directory. Example: /public/index.html
Express 4.x
发送.html 文件,没有模板引擎...
Express 4.x
Send .html files, no template engine...
我添加了下面 2 行,它对我有用
I added below 2 line and it work for me
尝试 Express 路线中的 res.sendFile() 函数。
阅读此处:http://codeforgeek.com/2015/01/render-html -文件-expressjs/
Try res.sendFile() function in Express routes.
Read here : http://codeforgeek.com/2015/01/render-html-file-expressjs/
我不想依赖 ejs 来简单地交付 HTML 文件,所以我自己编写了这个微型渲染器:
I didn't want to depend on ejs for simply delivering an HTML file, so I simply wrote the tiny renderer myself:
非常遗憾的是,大约到了 2020 年,express 仍然没有添加一种不使用
response
对象的sendFile
方法来渲染 HTML 页面的方法。使用sendFile
不是问题,但以path.join(__dirname, 'relative/path/to/file')
的形式向其传递参数感觉不正确。为什么用户应该将__dirname
添加到文件路径?它应该是默认完成的。为什么服务器的根目录不能默认为项目目录?另外,仅仅为了渲染静态 HTML 文件而安装模板依赖项也是不正确的。我不知道解决这个问题的正确方法,但是如果我必须提供静态 HTML,那么我会这样做:上面的示例假设项目结构有一个
views
目录,并且静态 HTML 文件位于其中。例如,假设views
目录有两个名为index.html
和about.html
的 HTML 文件,那么要访问它们,我们可以访问:localhost:8153/index.html
或仅localhost:8153/
加载index.html
页面和localhost:8153 /about.html
加载about.html
。我们可以使用类似的方法来服务 React/Angular 应用程序,方法是将工件存储在views
目录中,或者仅使用默认的dist/
目录,在服务器js中配置如下:It is very sad that it is about 2020 still express hasn't added a way to render an HTML page without using
sendFile
method of theresponse
object. UsingsendFile
is not a problem but passing argument to it in the form ofpath.join(__dirname, 'relative/path/to/file')
doesn't feel right. Why should a user join__dirname
to the file path? It should be done by default. Why can't the root of the server be by defalut the project directory? Also, installing a templating dependency just to render a static HTML file is again not correct. I don't know the correct way to tackle the issue, but if I had to serve a static HTML, then I would do something like:The above example assumes that the project structure has a
views
directory and the static HTML files are inside it. For example, let's say, theviews
directory has two HTML files namedindex.html
andabout.html
, then to access them, we can visit:localhost:8153/index.html
or justlocalhost:8153/
to load theindex.html
page andlocalhost:8153/about.html
to load theabout.html
. We can use a similar approach to serve a react/angular app by storing the artifacts in theviews
directory or just using the defaultdist/<project-name>
directory and configure it in the server js as follows:1)
最好的方法是设置静态文件夹。在你的主文件(app.js | server.js | ???)中:
public/css/form.html
public/css/style.css
然后你从“public”文件夹中获得静态文件:
2)
你可以创建你的文件缓存。
使用方法 fs.readFileSync
1)
The best way is to set static folder. In your main file (app.js | server.js | ???):
public/css/form.html
public/css/style.css
Then you got static file from "public" folder:
2)
You can create your file cache.
Use method fs.readFileSync
我试图使用快速的 RESTful API 设置一个有角度的应用程序,并多次登陆此页面,尽管它没有帮助。以下是我发现有效的方法:
然后在 api 路由的回调中如下所示:
res.jsonp(users);
您的客户端框架可以处理路由。 Express 用于服务 API。
我回家的路线是这样的:
I was trying to set up an angular app with an express RESTful API and landed on this page multiple times though it wasn't helpful. Here's what I found that worked:
Then in the callback for your api routes look like:
res.jsonp(users);
Your client side framework can handle routing. Express is for serving the API.
My home route looks like this:
将以下行添加到您的代码中
将“jade”替换为“ejs”& package.json 文件中带有“*”的“XYZ”(版本)
然后在您的 app.js 文件中添加以下代码:
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
记住将所有 .HTML 文件保留在views文件夹中
干杯 :)
Add the following Lines to your code
Replace "jade" with "ejs" & "X.Y.Z"(version) with "*" in package.json file
Then in your app.js File Add following Code :
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
And Remember Keep All .HTML files in views Folder
Cheers :)
这是 Express 服务器的完整文件演示!
https://gist .github.com/xgqfrms-GitHub/7697d5975bdffe8d474ac19ef906e906
希望对你有帮助!
Here is a full file demo of express server!
https://gist.github.com/xgqfrms-GitHub/7697d5975bdffe8d474ac19ef906e906
hope it will help for you!
将您的index.html文件放在公共文件夹中
现在在终端中运行以下代码
Put your index.html file in public folder
Now run the following code in your terminal
对于纯 html,你不需要任何 npm 包或中间件,
只需使用这个:
For plain html you don't require any npm package or middleware
just use this:
我希望允许对“/”的请求由 Express 路由处理,而以前这些请求是由静态中间件处理的。这将允许我渲染 index.html 的常规版本或加载串联 + 缩小的 JS 和 CSS 的版本,具体取决于应用程序设置。受到 Andrew Homeyer 的回答的启发,我决定将我的 HTML 文件 - 未修改 - 拖到视图文件夹中,像这样配置 Express
并且创建了一个像这样的路由处理程序
这效果很好。
I wanted to allow requests to "/" to be handled by an Express route where previously they had been handled by the statics middleware. This would allow me to render the regular version of index.html or a version that loaded concatenated + minified JS and CSS, depending on application settings. Inspired by Andrew Homeyer's answer, I decided to drag my HTML files - unmodified - into a views folder, configure Express like so
And created a route handler like so
This worked out pretty well.
在 server.js 中,请包含
In server.js, please include
如果您尝试提供一个已经包含所有内容的 HTML 文件,那么它不需要“渲染”,只需要“提供”即可。渲染是指在将页面发送到浏览器之前让服务器更新或注入内容,并且它需要其他依赖项(例如 ejs),如其他答案所示。
如果您只是想根据浏览器的请求将浏览器定向到文件,则应该使用 res .sendFile() 像这样:
这样除了express之外你不需要额外的依赖项。如果您只想让服务器发送您已经创建的 html 文件,上面的方法是一种非常轻量级的方法。
If you are trying to serve an HTML file which ALREADY has all it's content inside it, then it does not need to be 'rendered', it just needs to be 'served'. Rendering is when you have the server update or inject content before the page is sent to the browser, and it requires additional dependencies like ejs, as the other answers show.
If you simply want to direct the browser to a file based on their request, you should use res.sendFile() like this:
This way you don't need additional dependencies besides express. If you just want to have the server send your already created html files, the above is a very lightweight way to do so.
现在我们在 NodeJS 中使用模块。
导入包
Nowadays we use modules in NodeJS.
Import packages