9

シンプルなサーバー ロジック、ビュー、多数のクライアント側 JS など、ごく普通の Express アプリであるアプリがあります。多くの AJAX リクエストを実行する必要があります。それらのいくつかは、HTTPS プロトコルで保護する必要があります (必要でないものもあります)。

したがって、私のサーバーは HTTP と HTTPS の両方で動作するはずです。また、ローカル マシン (通常は nodemon で実行) と Heroku の両方で動作するはずです。

私が理解している限り、Heroku はリッスンできる単一のポート (process.env.PORT) を提供し、プロキシを介してすべてのリクエストを処理します (したがって、アプリはこのポートをリッスンし、proto を気にしません。そうですか? )

それで、私はこれを正しく理解していますか?開発マシンとHerokuには別のコードが必要ですか?

お気に入り

...
app = express()
...

if process.env.NODE_ENV == 'production'
  app.listen(process.env.PORT)
else
  https = require('https')
  http = require('http')
  http.createServer(app).listen(5080) # some local port
  options = {
    key: fs.readFileSync('key.pem'), 
    cert: fs.readFileSync('cert.pem') # my self-signed files
  }
  https.createServer(options, app).listen(5443) # some different local port

これに対処する適切な方法はありますか?

4

3 に答える 3

12

Coffeescript-challenges については、Javascript に変換された Guard の回答のバージョンを次に示します。if else ステートメントを分割するために別のアプローチを取りました。

var express = require('express');
var http = require('http');
var https = require('https');
var fs = require('fs');
var privateKey = fs.readFileSync('./config/localhost.key').toString();
var certificate = fs.readFileSync('./config/localhost.crt').toString();

var options = {
  key : privateKey
, cert : certificate
}

var app = express();

// Start server.
var port = process.env.PORT || 3000; // Used by Heroku and http on localhost
process.env['PORT'] = process.env.PORT || 4000; // Used by https on localhost

http.createServer(app).listen(port, function () {
    console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});

// Run separate https server if on localhost
if (process.env.NODE_ENV != 'production') {
    https.createServer(options, app).listen(process.env.PORT, function () {
        console.log("Express server listening with https on port %d in %s mode", this.address().port, app.settings.env);
    });
};

if (process.env.NODE_ENV == 'production') {
    app.use(function (req, res, next) {
        res.setHeader('Strict-Transport-Security', 'max-age=8640000; includeSubDomains');
        if (req.headers['x-forwarded-proto'] && req.headers['x-forwarded-proto'] === "http") {
            return res.redirect(301, 'https://' + req.host + req.url);
        } else {
            return next();
            }
    });
} else {
    app.use(function (req, res, next) {
        res.setHeader('Strict-Transport-Security', 'max-age=8640000; includeSubDomains');
        if (!req.secure) {
            return res.redirect(301, 'https://' + req.host  + ":" + process.env.PORT + req.url);
        } else {
            return next();
            }
    });

};
于 2013-04-29T04:30:50.463 に答える
8

さて、コミュニティは最近かなり死んでいるように見えます(私が間違っていることを願っています)

答えは次のとおりです。

a)はい、これはそれに対処する方法です

b)セキュアモードであるかどうかを確認する方法は、環境によっても異なります。

if process.env.NODE_ENV == 'production'
  is_secure = (req) ->
    req.headers['x-forwarded-proto'] == 'https'
else
  is_secure = (req) -> req.secure

追加 HTTPSを強制する場合:

redirect_to_https = (req, res, next) ->
  if not is_secure(req)
    res.redirect config.SECURE_DOMAIN + req.url
  else
    next()

app
  .use(redirect_to_https)
于 2012-11-12T20:43:30.070 に答える
3

を使用するapp.enable('trust proxy')と、req.secureブール値 (http/https) が Heroku でも動作し、互換性のある SSL ターミネーション プロキシの背後でも動作します。

于 2014-05-21T09:36:52.770 に答える