我遇到了一个问题,我无法设置支持ssl的节点js服务器,也可以通过此ssl提供静态文件,当我添加 app.use(express.static( __dirname +' / public'))这些文件是从https常规服务器提供的。 有人试图这样做吗?
//require engines
var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser')
//init express engine
var app = express();
//using
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
var activeport = process.env.PORT ? process.env.PORT : 4003;
if (process.env.NODE_ENV === "production") {
var fs = require('fs');
var https = require('https');
var options = {
key: fs.readFileSync(global.appRoot + '/xxx.pem'),
cert: fs.readFileSync(global.appRoot + '/yyy.crt'),
passphrase: "123123",
requestCert: false,
rejectUnauthorized: false
};
var server = https.createServer(options, app).listen(activeport, function () {
console.log('Server listening on port ' + activeport);
});
}
else {
app.listen(activeport, function () {
console.log('Server listening on port ' + activeport);
});
}
答案 0 :(得分:0)
试试这个:
const path = require('path'); // <--- Require this
app.use(express.static(path.join(__dirname, '/dist')));
答案 1 :(得分:0)
好的,明白了。 我使用Azure作为我的托管服务提供商,Azure为您提供所有SSL支持,因此我不需要创建专用的 https 服务器:
var server = https.createServer(options, app).listen(activeport, function () {
console.log('Server listening on port ' + activeport);
});
我需要做的就是添加我的web.config以下代码 - 永久重定向到https网址。
<!-- Redirect all traffic to SSL -->
<rule name="Force HTTPS" enabled="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
Referar: How do you force express on node.js in Azure Websites to use https?