gpt4 book ai didi

node.js - 如何在 Node.js 中从模块中获取变量 - 出现语法错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:17:12 25 4
gpt4 key购买 nike

我有一个简单的 node.js https 服务器,我想将证书部分提取到它自己的模块中。但我收到语法错误,我不确定我做错了什么..

certificate.cert
^
SyntaxError: Unexpected token '.'

app.js

const https = require('https')
const express = require('express')
const certificate = require('./certificate.js')

const app = express()

const server = https.createServer({
certificate.cert,
certificate.key,
}, app)

server.listen(443)

certificate.js

const fs = require('fs')

var certificate = {
cert: fs.readFileSync('/etc/letsencrypt/live/www.4evergaming.com/cert.pem'),
key: fs.readFileSync('/etc/letsencrypt/live/www.4evergaming.com/privkey.pem'),
}

exports.certificate = certificate

当我console.log(certificate)时,我得到这个输出

{
certificate: {
cert: <Buffer 2d 2d 2d 2d 2d 51 12 47 49 4e 20 43 13 52 54 49 46 11 43 41 54 33 2d 2d 2d 2d 2d 0a 4d 49 49 46 75 54 40 43 42 4b 47 60 41 77 49 42 44>,
key: <Buffer 2d 2d 2d 2d 2d 61 32 12 50 4e 30 40 52 49 56 41 54 45 20 4b 45 59 2d 2d 2d 2d 2d 0a 31 49 49 45 61 51 49 40 41 44 41 4e 42 67 6b 71 68 6c>,
}
}

最佳答案

尝试:

fs.readFileSync('/etc/letsencrypt/live/www.4evergaming.com/cert.pem', 'utf8')

如果未定义编码,readFileSync 返回 Buffer

关于node.js - 如何在 Node.js 中从模块中获取变量 - 出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60502628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com