简体   繁体   English

在Express中呈现HTML时出错

[英]Error when rendering HTML in express

I'm trying out a simple one page render and I'm obviously doing it wrong: 我正在尝试一个简单的一页渲染,但显然做错了:

var express = require('express');
var app = express();

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

app.listen(3000);
console.log('Listening on port 3000');

Error: 错误:

Error: Cannot find module 'html'

I want to load a page with CSS/images and wanted to know where to store the CSS/images/scripts/HTML and how to load it. 我想用CSS / images加载页面,并想知道CSS / images / scripts / HTML的存储位置以及如何加载。 I have yet to find many example express apps. 我尚未找到许多示例快递应用程序。

When using res.render , you are rendering a defined view. 使用res.render ,正在渲染定义的视图。 The documentation state: 文档状态:

Render a view with a callback responding with the rendered string. 使用回调响应渲染的字符串来渲染视图。 When an error occurs next(err) is invoked internally. 发生错误时,将在内部调用next(err)。

For specific HTML, you either have to load the file with fs.readFile , or have Express use a rendering engine, such as Jade or EJS. 对于特定的HTML,您必须使用fs.readFile加载文件,或者让Express使用呈现引擎,例如Jade或EJS。 For example, this code sample would apply the EJS render engine to all files with the extension .html . 例如,此代码示例将EJS渲染引擎应用于所有扩展名为.html文件。

app.engine('html', require('ejs').renderFile);

Then you can render a file like this. 然后,您可以渲染这样的文件。 This is an example of passing variables to the HTML file, because that's what embedded JavaScript is used for. 这是将变量传递到HTML文件的示例,因为这就是嵌入式JavaScript的用途。

app.get('/', function (req, res) {
  res.render('/file', {
    pass: 'arguments',
    to: 'the file here'
  });
});

That is more convenient than having to read the file on each load like so: 这比每次读取负载都要方便,例如:

app.get('/', function (req, res) {
  fs.readFile('/index.html', 'utf8', function (err, data) {
    res.send(data);
  });
});

Although this is aside from your question, this is how passing variables to EJS works. 尽管这不是您的问题,但这是将变量传递给EJS的方式。 It even works with if statements and loops , since it accepts all JavaScript. 它甚至可以与if语句和loops ,因为它接受所有JavaScript。 Putting console.log in the file would log each time the file was rendered. console.log放入文件中将在每次渲染文件时记录日志。

<span><%= pass %></span>
<!-- after rendering -->
<span>arguments</span>

If you need to serve other resources, such as CSS, JavaScript, or other html files, you could use Express' directory function. 如果需要提供其他资源,例如CSS,JavaScript或其他html文件,则可以使用Express的目录功能。

app.use(express.static('/'));

Then you wouldn't need to render your HTML at all. 然后,您根本不需要呈现HTML。 It would be statically served. 它将是静态投放的。

I want to load a html with css/images and wanted to know what is the layout of where to store the css/img/js and the html and load it? 我想用css / images加载html,并想知道在哪里存储css / img / js和html并加载它的布局是什么? I am yet to find many example express js apps. 我尚未找到许多示例快速js应用程序。

Assuming that you have installed express, within your express project you will find public folder. 假设你已经安装了快递,快递的项目中,你会发现公用文件夹 within public folder you will find three folder - images , javascripts , and stylesheets . 在公用文件夹中,您会找到三个文件夹- 图像javascript样式表 As the names suggest place your css/img/js in their respective folders. 顾名思义,将css / img / js放在各自的文件夹中。

You place static HTML files not using any templates in the in the express project public folder and invoke it using: 您将不使用任何模板的静态HTML文件放置在express项目的公共文件夹中,并使用以下方法调用它:

app.get('/', function(req, res) {
  res.redirect('/home.html');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM