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

标签 node.js ssl https server

我有一个简单的 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/

相关文章:

ubuntu - 在 ubuntu 18.04.3 nginx 上设置 SSL

node.js - 让 browserify-shim 按预期工作

node.js - 我需要为每个用户单独管理 jwt key 吗?

node.js - Passport-local-mongoose 更改密码函数

node.js - 如何让值对象远离服务器?

python - 向请求添加单个证书

Apache 2.4 转发代理配置问题

security - 我们构建的 CRM 应用程序需要什么 SSL 证书?

http - 加密电子邮件 VS SSL 数据传输 : which is better?

WordPress HTTP 请求 501 内部服务器错误