Node.js代码在html文件中

时间:2021-07-14 15:26:03

It is possible to mix HTML with node.js commands? I want to make my site like in PHP so:

可以将HTML与node.js命令混合使用吗?我想让我的网站像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. I need this, because I want to get data from MySQL database and then show it on my site.

因此,服务器生成所有命令,这些命令包含在HTML文件中,而不是返回纯HTML以在用户浏览器中显示它。我需要这个,因为我想从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。

4 个解决方案

#1


2  

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

#2


1  

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. The point is how to generate the string with html syntax.

您可以使用html语法生成一些字符串,例如“ .. ”,然后将其放入res.write中,然后使用html文件获得响应。关键是如何使用html语法生成字符串。

If we have a template file like this:

如果我们有这样的模板文件:

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

We want to get the "Hello world!" between <h1> and </h1>. So we can have the work done by:

我们希望获得“Hello world!”在

和 之间。所以我们可以通过以下方式完成工作:

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

After doing this, you can get the string with html syntax. and that is what most template engines(like ejs, jade) do, of course they have more complex works to do.

执行此操作后,您可以使用html语法获取字符串。这就是大多数模板引擎(如ejs,jade)所做的,当然他们有更复杂的工作要做。

I hope that this can help you know more about template engine with node.js, and please forgive my poor English...

我希望这可以帮助你了解更多关于node.js模板引擎的信息,请原谅我糟糕的英语......

#3


0  

Your example using Express and EJS (which I use). Jade seems like overkill to me, but that's just my opinion.

您的示例使用Express和EJS(我使用)。翡翠对我来说似乎有些过分,但这只是我的看法。

// 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. 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.

这几乎是使用EJS的基础知识。我个人喜欢Jade,因为我用来制作html的人不会用翡翠格式把它交给我,而且它与我以前用于模板制作的方式非常相似。

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. You just need to dig into the docs and examples on the web.

当然,你可以用模板做更多的事情,你可以在其中加入javascript,循环数据库返回的数组,包含其他.ejs文件。您只需要深入了解网络上的文档和示例。

#4


0  

Have you tried a web application framework like express?

您是否尝试过像express这样的Web应用程序框架?

Check it out here!

看看这里!

#1


2  

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

#2


1  

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. The point is how to generate the string with html syntax.

您可以使用html语法生成一些字符串,例如“ .. ”,然后将其放入res.write中,然后使用html文件获得响应。关键是如何使用html语法生成字符串。

If we have a template file like this:

如果我们有这样的模板文件:

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

We want to get the "Hello world!" between <h1> and </h1>. So we can have the work done by:

我们希望获得“Hello world!”在

和 之间。所以我们可以通过以下方式完成工作:

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

After doing this, you can get the string with html syntax. and that is what most template engines(like ejs, jade) do, of course they have more complex works to do.

执行此操作后,您可以使用html语法获取字符串。这就是大多数模板引擎(如ejs,jade)所做的,当然他们有更复杂的工作要做。

I hope that this can help you know more about template engine with node.js, and please forgive my poor English...

我希望这可以帮助你了解更多关于node.js模板引擎的信息,请原谅我糟糕的英语......

#3


0  

Your example using Express and EJS (which I use). Jade seems like overkill to me, but that's just my opinion.

您的示例使用Express和EJS(我使用)。翡翠对我来说似乎有些过分,但这只是我的看法。

// 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. 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.

这几乎是使用EJS的基础知识。我个人喜欢Jade,因为我用来制作html的人不会用翡翠格式把它交给我,而且它与我以前用于模板制作的方式非常相似。

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. You just need to dig into the docs and examples on the web.

当然,你可以用模板做更多的事情,你可以在其中加入javascript,循环数据库返回的数组,包含其他.ejs文件。您只需要深入了解网络上的文档和示例。

#4


0  

Have you tried a web application framework like express?

您是否尝试过像express这样的Web应用程序框架?

Check it out here!

看看这里!