Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js HTTPS上的Express静态目录_Node.js_Express_Https_Static - Fatal编程技术网

Node.js HTTPS上的Express静态目录

Node.js HTTPS上的Express静态目录,node.js,express,https,static,Node.js,Express,Https,Static,我想使用Express通过HTTPS实现Node.js,用于静态内容。浏览Web会发现大量使用HTTPS的Express示例,以及大量使用静态目录的Express示例,但我找不到使用所有三种Express、HTTPS和静态目录的示例 此外,看看我能找到的例子,我无法拼凑出如何做到这一点 以下是我的发现: 通过HTTP表示静态目录 HTTPS上的Express(无静态目录) 当我尝试结合这两种方法时,我陷入了困境,因为静态示例绕过了createServer,而createServer是示例中获取H

我想使用Express通过HTTPS实现Node.js,用于静态内容。浏览Web会发现大量使用HTTPS的Express示例,以及大量使用静态目录的Express示例,但我找不到使用所有三种Express、HTTPS和静态目录的示例

此外,看看我能找到的例子,我无法拼凑出如何做到这一点

以下是我的发现:

通过HTTP表示静态目录

HTTPS上的Express(无静态目录)

当我尝试结合这两种方法时,我陷入了困境,因为静态示例绕过了createServer,而createServer是示例中获取HTTPS的关键


我相信答案很简单,但我无法找到或找到解决方案。

能否尝试下面的代码片段,看看它是否适合您

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

const app = express();
app.use(express.static(process.env.SERVE_DIRECTORY));

app.get('/', function(req, res) {
    return res.end('Serving static files!');
});

const key = fs.readFileSync(__dirname + '/selfsigned.key');
const cert = fs.readFileSync(__dirname + '/selfsigned.crt');

const options = {
    key: key,
    cert: cert
};

const server = https.createServer(options, app);

server.listen(PORT, () => {
    console.log(`Serving on port ${PORT}`);
});

请确保在运行上述代码之前进行适当的更改。

您是否检查了此链接:?这看起来很有希望,谢谢。我会在睡了几个小时后接近它。非常感谢。管理员注意:我本想编辑我自己的原始帖子,但无意中试图编辑解决方案。请忽略尝试的编辑,您也可以删除此评论。谢谢
const app = require('express')();
const https = require('https');
const server = https.createServer(
        {
        key: fs.readFileSync('server.key'),
        cert: fs.readFileSync('server.cert')
        },
        app
    );
server.listen(APIPort);
const fs = require('fs');
const https = require('https');
const express = require('express');

const app = express();
app.use(express.static(process.env.SERVE_DIRECTORY));

app.get('/', function(req, res) {
    return res.end('Serving static files!');
});

const key = fs.readFileSync(__dirname + '/selfsigned.key');
const cert = fs.readFileSync(__dirname + '/selfsigned.crt');

const options = {
    key: key,
    cert: cert
};

const server = https.createServer(options, app);

server.listen(PORT, () => {
    console.log(`Serving on port ${PORT}`);
});