如何在nodejs中写一个HTML文件

How to write a HTML file in nodejs

所以我生成了一些 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!')
})

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

const html = mjml2html(mjml)

const {html} = mjml2html(mjml)

一切都会好的