gpt4 book ai didi

javascript - 使用 Node.js 和 Express 创建 HTTPs 服务器

转载 作者:行者123 更新时间:2023-11-30 20:05:25 24 4
gpt4 key购买 nike

                   var https = require('https'),
fs = require('fs'),
express = require('express'),
app = express();
// cookieParser = require('cookie-parser'),
// path = require('path'),
// bodyParser = require('body-parser'),
// https = require('http');


var key = fs.readFileSync('encryption/star_massifsolutions_com.key');
var cert = fs.readFileSync('encryption/massif_wildcard.crt');
var ca = fs.readFileSync('encryption/DigiCertCA.crt');

var httpsOptions = {
key: key,
cert: cert,
ca: ca
};


https.createServer(httpsOptions, app).listen(8000, function () {
console.log("server running at https://IP_ADDRESS:8000/")
});
app.get('/', function (req, res) {
res.header('Content-type', 'text/html');
return res.end('Hello World!');
});

// app.set('view', __dirname + '/views');
// app.use(bodyParser.urlencoded({
// extended: true
// }));
// app.use(bodyParser.json({
// limit: '500mb'
// }));


// app.use('/', express.static(path.join(__dirname, '/dist/basic-structure')));

// app.get('/**', function (req, res, next) {
// console.log(req, res, next);
// res.sendFile('index.html', {
// root: __dirname + '/dist/basic-structure'
// });
// });

console.log("listening to port 8000");

在这里,我编写了 hello world 只是为了测试我的代码。所以在这种情况下,代码可以运行,但它不安全。我希望我的连接是安全的。在这种情况下,它显示已弃用的 http 并显示证书错误。但运行不安全。

同样,如果我将 hello world 部分替换为注释部分,如我的代码所示,它甚至不会使用已弃用的 http 运行。如果我将 https 替换为 http,它会运行。我需要帮助来运行我编辑的代码。如果我遗漏了一些要点,请告诉我。

简而言之,这段代码运行不安全,我想确保它安全

最佳答案

不确定我是否理解得很好,但是如果“代码未运行”是指您的应用程序,那么您的第二个代码集似乎只是尝试运行服务器而不是您的应用程序

据我了解,您将您的应用定义为 express 但您并未使用它,因此它不会交付

所以我的猜测是,您需要将 https 服务器命令与应用程序及其选项一起使用,以按照 @lx1412 的建议将所有内容(https 和应用程序)链接在一起

我会试试这个:

var express = require('express'),
cookieParser = require('cookie-parser'),
path = require('path'),
bodyParser = require('body-parser'),
// https = require('http'),
https = require('https'),
app = express(),
fs = require('fs');


var key = fs.readFileSync('encryption/star_massifsolutions_com.key');
var cert = fs.readFileSync( 'encryption/massif_wildcard.crt' );
var ca = fs.readFileSync( 'encryption/DigiCertCA.crt' );

var httpsOptions = {
key: key,
cert: cert,
ca: ca
};

app.set('view',__dirname+'/views');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json({limit: '500mb'}));


app.use('/', express.static(path.join(__dirname,'/dist/basic-structure')));

app.get('/**', function(req, res, next) {
console.log(req, res, next);
res.sendFile('index.html', { root: __dirname +
'/dist/basic-structure' });
});


// https.createServer(httpsOptions, (req, res) => {
// console.log("code works");
// res.writeHead(200);
// res.end('hello world\n');
// }).listen(8000);

https.createServer(httpsOptions, app).listen(8000, function () {
console.log("code works");
res.writeHead(200);
res.end('hello world\n');
});

编辑:

您能否简单地尝试一下,看看它的行为如何?另外,您能否提供已弃用的 http 和证书错误?

app.get('/', function (req, res) {
res.send('Hello World!');
});
https.createServer(httpsOptions, app).listen(8000, function () {
console.log("server running at https://IP_ADDRESS:8000/")
});

关于javascript - 使用 Node.js 和 Express 创建 HTTPs 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52984368/

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