如何使用docker在Node.js中创建HTTPS服务器?

时间:2020-06-07 12:24:36

标签: node.js docker amazon-ec2 https

我正在尝试使用node.js和and docker创建一个HTTPS服务器。我已经创建了证书并配置了应用程序。但是我不知道如何运行镜像泊坞窗。我正在尝试这个,但是没有用。 有谁知道该怎么做?

sudo docker run --name cont_docker -p 443:3333 --link my-mongo:my-mongo user/docker_app

我没有使用docker-compose

--->编辑<---

容器没有崩溃。它没有响应。在应用程序中,我得到“ couold not connect to server”。我认为它没有绑定端口

const express = require('express');

const getRootPath = require('../helpers/get-root-path.helper');

var fs = require('fs');
const https = require('https'); // REQUIRE HTTPS
const privateKey = fs.readFileSync('private/key/path', 'utf8');
const certificate = fs.readFileSync('cert/key/path', 'utf8');

let _express = null;
let _config = null;
let _router = null;
let _credentials = null;

class Server {

    constructor({ config, router }) {
        _router = router;
        _config = config;
        _credentials = { privateKey: privateKey, certificate: certificate } // ADD CREDENTIALS
        _express = express();
    }

    /*
        This methods returns a promisse that will be in charge of initate the server. 
    */
    start() {
        _express.use(express.static(`${this.getRootPath(__dirname)}/public`))
        _express.use(_router)
        _express.engine('html', require('ejs').renderFile)
        _express.set('view engine', 'html')

        return new Promise(resolve => {
        const server = https.createServer(this._credentials, _express)
            server.listen(_config.port, () => {
                console.log(`App running on port ${_config.port}`)
                resolve()
            })
        })
    }

    getRootPath(path) {
        return getRootPath(path)
    }
}

module.exports = Server

---> APP.JS <---

const container = require('./src/startup/container')

const server = container.resolve("app")

const { db_host, db_database } = container.resolve("config")

const mongoose = require('mongoose')

mongoose.set("useCreateIndex", true)

mongoose
    .connect(`mongodb://${db_host}/${db_database}`, { useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true, useFindAndModify: false })
    .then(response => {
        console.log(`Connected to Mongo! Database name: "${response.connections[0].name}" on "${response.connections[0].host}"`)
        server.start()
    })
    .catch(console.log)

0 个答案:

没有答案