繁体   English   中英

Nodejs将变量添加到导入的html字符串中

[英]Nodejs add variable into imported html string

您好,我目前在尝试将变量插入到从 readFileSync 获得的长 html 字符串中时遇到问题,我正在尝试在 ZFC35FDC70D5FC69D2692Z83A8 中的 href url 的末尾插入一个令牌。

我的测试。html:

<!DOCTYPE html>
<html
  style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">

<head>
  <meta name="viewport" content="width=device-width" />
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Actionable emails e.g. reset password</title>


  <style type="text/css">
    img {
      max-width: 100%;
    }

    body {
      -webkit-font-smoothing: antialiased;
      -webkit-text-size-adjust: none;
      width: 100% !important;
      height: 100%;
      line-height: 1.6em;
    }

    body {
      background-color: #f6f6f6;
    }

    @media only screen and (max-width: 640px) {
      body {
        padding: 0 !important;
      }

      h1 {
        font-weight: 800 !important;
        margin: 20px 0 5px !important;
      }

      h2 {
        font-weight: 800 !important;
        margin: 20px 0 5px !important;
      }

      h3 {
        font-weight: 800 !important;
        margin: 20px 0 5px !important;
      }

      h4 {
        font-weight: 800 !important;
        margin: 20px 0 5px !important;
      }

      h1 {
        font-size: 22px !important;
      }

      h2 {
        font-size: 18px !important;
      }

      h3 {
        font-size: 16px !important;
      }

      .container {
        padding: 0 !important;
        width: 100% !important;
      }

      .content {
        padding: 0 !important;
      }

      .content-wrap {
        padding: 10px !important;
      }

      .invoice {
        width: 100% !important;
      }
    }
  </style>
</head>

<body itemscope itemtype="http://schema.org/EmailMessage"
  style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em; background-color: #f6f6f6; margin: 0;"
  bgcolor="#f6f6f6">

  <table class="body-wrap"
    style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0;"
    bgcolor="#f6f6f6">
    <tr
      style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
      <td
        style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;"
        valign="top"></td>
      <td class="container" width="600"
        style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto;"
        valign="top">
        <div class="content"
          style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px;">
          <table class="main" width="100%" cellpadding="0" cellspacing="0" itemprop="action" itemscope
            itemtype="http://schema.org/ConfirmAction"
            style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px solid #e9e9e9;"
            bgcolor="#fff">
            <tr
              style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
              <td class="content-wrap"
                style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 20px;"
                valign="top">
                <meta itemprop="name" content="Confirm Email"
                  style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;" />
                <table width="100%" cellpadding="0" cellspacing="0"
                  style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                  <tr
                    style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                    <td class="content-block"
                      style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"
                      valign="top">
                      Please confirm your email address by clicking the link below.
                    </td>
                  </tr>
                  <tr
                    style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                    <td class="content-block"
                      style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"
                      valign="top">
                      We may need to send you critical information about our service and it is important that we have an
                      accurate email address.
                    </td>
                  </tr>
                  <tr
                    style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                    <td class="content-block" itemprop="handler" itemscope
                      itemtype="http://schema.org/HttpActionHandler"
                      style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"
                      valign="top">
                      <a href="https://91bf2bdc.ngrok.io/verify/?{token}` class=" btn-primary" itemprop="url"
                        style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #FFF; text-decoration: none; line-height: 2em; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 5px; text-transform: capitalize; background-color: #348eda; margin: 0; border-color: #348eda; border-style: solid; border-width: 10px 20px;">Confirm
                        email address</a>
                    </td>
                  </tr>
                  <tr
                    style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                    <td class="content-block"
                      style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"
                      valign="top">
                      &mdash; BB
                    </td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>
          <div class="footer"
            style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;">
            <table width="100%"
              style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
              <tr
                style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                <td class="aligncenter content-block"
                  style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;"
                  align="center" valign="top">Follow <a href="http://twitter.com/mail_gun"
                    style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; color: #999; text-decoration: underline; margin: 0;">@Mail_Gun</a>
                  on Twitter.</td>
              </tr>
            </table>
          </div>
        </div>
      </td>
      <td
        style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;"
        valign="top"></td>
    </tr>
  </table>
</body>

</html>

在 html 中,我将 href 放置在如下位置:

href="https://91bf2bdc.ngrok.io/verify/?{token}`

我的 import_html.js 文件:

const fs = require("fs");
const path = require("path");
const emailtemplate = fs.readFileSync(path.join(__dirname,'/test.html'), 'utf8');

const token = "eg2h4t41vn1j21h12gdd1212";

const emailString = eval('`'+emailtemplate+'`');


console.log(emailString);

当我打印 html 字符串时,我希望将令牌放在 url 的末尾,它应该包含以下内容:

href="https://91bf2bdc.ngrok.io/verify/eg2h4t41vn1j21h12gdd1212"

但是当我尝试使用节点运行时,它返回以下错误:

node import_html.js                                                    
at 09:31:13 undefined:138 <a href="https://91bf2bdc.ngrok.io/verify/?{token}` class=" btn-primary" itemprop="url"

^^^^^ SyntaxError: Unexpected token class

任何帮助将不胜感激:)

用replace代替eval不是更优雅吗?

 const emailtemplate = '<...previous htmlcode...><a href="https://91bf2bdc.ngrok.io/verify/?{token}<...end of htmlcode...>'; const token = "eg2h4t41vn1j21h12gdd1212"; const emailString = emailtemplate.replace(/\?{token}/,token); console.log(emailString);

您的模板字符串以双引号而不是反引号开头。 这就是为什么它在错误中显示 class 的原因,因为在=之后字符串当前被关闭

您的原始行:

href="https://91bf2bdc.ngrok.io/verify/?{token}`

将其更改为此应该可以解决它:

href=`https://91bf2bdc.ngrok.io/verify/?{token}`

暂无
暂无

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

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