简体   繁体   English

HTML文件中的Node.js代码

[英]Node.js code in html file

It is possible to mix HTML with node.js commands? 是否可以将HTML与node.js命令混合使用? I want to make my site like in PHP so: 我想使我的网站像在PHP中那样:

<html>
<!-- Some HTML -->
<?php 
    echo "example";
?>
</html>

So, server makes all commands, which are included in HTML file and than returns pure HTML to show it in users browser. 因此,服务器执行所有命令,这些命令包含在HTML文件中,然后返回纯HTML在用户浏览器中显示。 I need this, because I want to get data from MySQL database and then show it on my site. 我需要这个,因为我想从MySQL数据库中获取数据,然后将其显示在我的网站上。

In all tutorials I found only: 在所有教程中,我仅发现:

res.write("<Some html>");

And there is nothing about keeping html in separate files and add to them some server-side js. 而且没有任何关于将html保留在单独的文件中并向其中添加一些服务器端js的事情。

Find a templating engine and build your application around that, because what you want is not possible. 找到一个模板引擎并围绕该模板构建您的应用程序,因为无法实现所需的功能。 For a list of compatible template engines, take a look here: https://github.com/joyent/node/wiki/modules#wiki-templating 有关兼容模板引擎的列表,请在此处查看: https : //github.com/joyent/node/wiki/modules#wiki-templating

Use the function res.write is enough. 使用功能res.write就足够了。

You can generate some string with html syntax, like "<html>..</html>" , then you put it into the res.write and you get response with a html file. 您可以使用html语法生成一些字符串,例如"<html>..</html>" ,然后将其放入res.write并获得带有html文件的响应。 The point is how to generate the string with html syntax. 关键是如何使用html语法生成字符串。

If we have a template file like this: 如果我们有这样的模板文件:

<html>
  <body>
    <h1>{{ "Hello" + " world!" }}</h1>
  </body>
</html>

We want to get the "Hello world!" 我们想要获得"Hello world!" between <h1> and </h1> . <h1></h1> So we can have the work done by: 因此,我们可以通过以下方式完成工作:

  1. read the template file with fs.readFile 使用fs.readFile读取模板文件
  2. use regular expression to get the content between {{ and }} 使用正则表达式获取{{}}之间的内容
  3. use eval to evaluate the content, and replace them. 使用eval评估内容,并将其替换。

After doing this, you can get the string with html syntax. 完成此操作后,您可以使用html语法获取字符串。 and that is what most template engines(like ejs , jade ) do, of course they have more complex works to do. 这就是大多数模板引擎(例如ejsjade )所做的事情,当然它们还有更复杂的工作要做。

I hope that this can help you know more about template engine with node.js, and please forgive my poor English... 我希望这可以帮助您了解更多有关使用node.js的模板引擎的信息,请原谅我可怜的英语...

Your example using Express and EJS (which I use). 您的示例使用Express和EJS(我使用的是)。 Jade seems like overkill to me, but that's just my opinion. 玉在我看来似乎太过分了,但这只是我的看法。

// index.ejs (the html template)
<html>
    <head></head>
    <body>
        <div><%= example %></div>
    </body>
</html>

And in your node app: 在您的节点应用中:

app.get('/', function (req, res, next) {
    res.render('index.ejs', {
        layout: false,
        locals: {
            example: "Hello world!"
        }
    });
});

That's pretty much the basics of using EJS. 这几乎就是使用EJS的基础。 I personally like it over Jade because the people I use to do up the html don't hand it to me in Jade format, and it's very similar to how I used to do php templating. 我个人比较喜欢Jade,因为我用来编写html的人不会以Jade格式将它交给我,这与我以前进行php模板制作的方式非常相似。

There is more you can do with the templates of course, you can put javascript in them to say, loop through an array returned by a database, include other .ejs files. 当然,您还可以使用模板做更多的事情,您可以在其中添加javascript,以遍历数据库返回的数组,包括其他.ejs文件。 You just need to dig into the docs and examples on the web. 您只需要深入研究Web上的文档和示例。

Have you tried a web application framework like express? 您是否尝试过像express这样的web application framework

Check it out here! 在这里查看!

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

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