gpt4 book ai didi

javascript - 如何为在 Express 中运行的应用程序配置 SSL 证书?

转载 作者:行者123 更新时间:2023-11-30 14:28:50 25 4
gpt4 key购买 nike

我正在尝试配置我创建的 Express 服务器,以传递 SSL 证书并从 http 转到 https。

我阅读了 Express 文档,但找不到解决方案。他们向我提出了像 Lets Encrypt 这样的东西,但它不支持 Node.js 我不知道我是否应该修改我已经修改过的主机文件来运行应用程序,或者我必须做什么。我看到了一个表格,但它只适用于 Unix 系统。我展示了我配置服务器文件的方式,以防他们能帮助我,我花了三天时间寻找方法,但没有成功。我看到的那些不支持 Node.js。谢谢

I EDIT THE QUESTION: Sorry for not including more details, the question is that my application is not in production and my domain is provisional: michaelgram.test. I think that with that Lets Encrypt does not grant me the certificates. I do not know what else to do.The issue is that the application is hosted locally, on my computer

I edit again: Forgive, forget to say that my purpose is to create the certificate for an application in which you can make the registration to Facebook and tried the methods that my colleagues kindly offered, but it did not work, thanks to the new facebook policy. If you have another idea, then my domain would be michaelgram.test thank you and forgive the inconvenience, for not doing well the question.

let express = require('express');
let aws = require('aws-sdk');
let multer = require('multer');
let multerS3 = require('multer-s3');
let ext = require('file-extension');
let cookieParser = require('cookie-parser');
let bodyParser = require('body-parser');
let expressSession = require('express-session');
let passport = require('passport');
let michaelgram = require('michaelgram-client');
let auth = require('./auth')
let config = require('./config');
let port = process.env.PORT || 5050;

let client = michaelgram.createClient(config.client);

let s3 = new aws.S3({
accessKeyId: config.aws.accessKey,
secretAccessKey: config.aws.secretKey
});

let storage = multerS3({
s3: s3,
bucket: 'michaelgram',
acl: 'public-read',
metadata: function (req, file, cb) {
cb(null, { fieldName: file.fieldname })
},
key: function (req, file, cb) {
cb(null, +Date.now() + '.' + ext(file.originalname))
}
});


let upload = multer({ storage: storage }).single('picture');

let app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(expressSession({
secret: config.secret,
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
app.set('view engine', 'pug');
app.use(express.static('public'));

passport.use(auth.localStrategy);
passport.use(auth.facebookStrategy);
passport.deserializeUser(auth.deserializeUser);
passport.serializeUser(auth.serializeUser);

app.get('/', function (req, res) {
res.render('index', { title: 'Michaelgram' });
})

app.get('/signup', function (req, res) {
res.render('index', { title: 'Michaelgram - Signup' });
})

app.post('/signup', function (req, res) {
let user = req.body;
client.saveUser(user, function (err, usr) {
if (err) return res.status(500).send(err.message)
debugger
res.redirect('/signin');
});
});

app.get('/signin', function (req, res) {
res.render('index', { title: 'Michaelgram - Signin' });
})

app.post('/login', passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/signin'
}));

app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));

app.get('/auth/facebook/callback', passport.authenticate('facebook', {
successRedirect: '/',
failureRedirect: '/signin'
}));

function ensureAuth (req, res, next) {
if (req.isAuthenticated()) {
return next()
}

res.status(401).send({ error: 'not authenticated' })
}

app.get('/api/pictures', function (req, res, next) {
let pictures = [ ];

setTimeout(function () {
res.send(pictures);
}, 2000)
});

app.post('/api/pictures', ensureAuth,function (req, res) {
upload(req, res, function (err) {
if (err) {
return res.send(500, "Error uploading file");
}
res.send('File uploaded');
})
})

app.get('/api/user/:username', (req, res) => {
const user = {
username: 'miguelito',
avatar: '',
pictures: [ ]
}

res.send(user);
})

app.get('/:username', function (req, res) {
res.render('index', { title: `Michaelgram - ${req.params.username}` });
})

app.get('/:username/:id', function (req, res) {
res.render('index', { title: `Michaelgram - ${req.params.username}` });
})

app.listen(port, function (err) {
if (err) return console.log('Hubo un error'), process.exit(1);

console.log('Michaelgram escuchando en el puerto 5050');
})

最佳答案

当您使用 TLS 保护网络服务器时,您需要做两件事:

  • 私钥
  • 服务器证书

关于您的第一点,Lets Encrypt 是一项将完全支持您尝试做的事情的服务。他们提供的服务允许您生成受信任的 key 和证书,以保护服务器上的流量AS WELL AS 让其他人知道它是由受信任的证书颁发机构签署的。参见 https://letsencrypt.org/how-it-works/

如果您只是想要 tls,您可以像这样生成一个自签名证书: https://www.akadia.com/services/ssh_test_certificate.html

在您获得证书和 key 后,这里是服务器的 https 配置:

var https = require('https');
var fs = require('fs');
var express = require('express');

var options = {
key: fs.readFileSync('/etc/apache2/ssl/server.key'),
cert: fs.readFileSync('/etc/apache2/ssl/server.crt'),
requestCert: false,
rejectUnauthorized: false
};


var app = express();

var server = https.createServer(options, app).listen(3000, function(){
console.log("server started at port 3000");
});

参见:create a trusted self-signed SSL cert for localhost (for use with Express/Node)

关于javascript - 如何为在 Express 中运行的应用程序配置 SSL 证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51522632/

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