require file as string

后端 未结 5 1112
日久生厌
日久生厌 2020-12-13 03:33

I\'m using node + express and I am just wondering how I can import any file as a string. Lets say I have a txt file all I want is to load it into a variable as such.

5条回答
  •  [愿得一人]
    2020-12-13 03:42

    To read the CSS file to String, use this code. It works for .txt.

    const fs = require('fs')
    const path = require('path')
    
    const css = fs.readFileSync(path.resolve(__dirname, 'email.css'), 'utf8')
    

    ES6:

    import fs from 'fs'
    import path from 'path'
    
    let css = fs.readFileSync(path.resolve(__dirname, 'email.css'), 'utf8')
    

提交回复
热议问题