【问题标题】:How to write a HTML file in nodejs如何在 nodejs 中编写 HTML 文件
【发布时间】:2021-02-12 00:34:40
【问题描述】:

所以我生成了一些 HTML,并且我有一个包含名为 html 的 HTML 的对象,我想将其写入一个新文件,但现在它不起作用,因为我保存的文件中只有 NaN。这就是我目前所拥有的

import mjml2html from 'mjml'
import Handlebars from 'handlebars'
import fs from 'fs'

const template = Handlebars.compile(`
  <mjml>
  <mj-body>
    <mj-section background-color="#F0F0F0" padding-bottom="0">
      
      <mj-column  padding-left="70px" width="250px">
        
        <mj-text font-style="italic" font-size="22px" color="#626262">watFriends</mj-text>
        
      </mj-column>
      
      <mj-column width="170px"> 
            <mj-image width="30px" src={{logo}} />
        </mj-column>
    </mj-section>
    
    <mj-section background-color="#FAFAFA">
      <mj-column width="400px">
        <mj-text font-style="italic" font-size="15px" font-family="Helvetica Neue" color="#626262">
          Dear {{firstName}},
        </mj-text>
        <mj-text color="#525252">{{message}}
        </mj-text>
      </mj-column>
    </mj-section>
  </mj-body>
</mjml>
    
`)
const context = {
  firstName: '',
  message: 'hello',
  logo: 'logo.png',
}
const mjml = template(context)
const html = mjml2html(mjml)
console.log(html)

fs.writeFile('new.html', html.toString(), { encoding: 'utf8' }, function (err) {
  if (err) {
    return console.log(err)
  }
  console.log('The file was saved!')
})

【问题讨论】:

    标签: javascript node.js email mjml


    【解决方案1】:

    mjml2html returns an object like {html: ' ...html here..', json: {}, errors: []} 和此对象的 toString() 是您正在写入文件的字符串 "[Object object]"。 改变

    const html = mjml2html(mjml)
    

    const {html} = mjml2html(mjml)
    

    一切都会好起来的

    【讨论】:

    • 非常感谢,这正是我所需要的!
    猜你喜欢
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 2014-08-21
    • 2020-06-27
    • 2013-12-14
    • 2019-10-30
    • 1970-01-01
    • 2016-12-10
    相关资源
    最近更新 更多