代码之家  ›  专栏  ›  技术社区  ›  Øystein Amundsen

在Node Express应用程序中使用letsencrypt

  •  2
  • Øystein Amundsen  · 技术社区  · 8 年前

    // Express
    import * as http from 'http';
    import * as https from 'https';
    import * as e from 'express';
    const LEX = require('greenlock-express');
    
    /** Run this using MyServer.Initialize(process.argv) **/
    export class MyServer {
      public app: e.Express;
      private clientPath = path.join(__dirname, './public');
    
      static Initialize(args: string[]): Promise<any> {
        return new MyServer()
          .start(args)
          .catch((error: any) => console.error(error);
      }
    
      constructor() { }
    
      start(args: string[]): Promise<https.Server> {
        // Simplified the code here, in order to only display relevant info
        return Promise.resolve(this.createServer());
      }
    
      public createServer(): https.Server {
        this.app = e(); // Creates an express application
    
        // Setup routes and standard stuff, which I removed to keep this question simple
    
        // Setup base route to everything else
        this.app.get('/*', (req: e.Request, res: e.Response) => {
          res.sendFile(path.resolve(this.clientPath, 'index.html'));
        });
    

    http://localhost .

        return this.app.listen(80)
          .on('listening', () => console.log(`Serving on http://localhost/`));
    

    https://www.npmjs.com/package/greenlock-express ,因此,我有点失望,我无法让它开箱即用):

        const lex = LEX.create({
          server: 'staging',
          email: 'my.email@mailprovider.com',
          agreeTos: true,
          configDir: 'cert/',
          approveDomains: ['mydomain.org']
        });
    
        // Force redirect to https
        http.createServer(lex.middleware(require('redirect-https')())).listen(80, function () {
          console.log('Listening for ACME http-01 challenges on', this.address());
        });
    
        return <https.Server> https.createServer(lex.httpsOptions, lex.middleware(this.app))
          .listen(443, () => console.log(`Serving on http://localhost/`));
      }
    }
    

    http://localhost https://localhost ,但在此之后,浏览器放弃并声明 The site cannot be reached .

    为什么?我误解了什么吗?

    我知道letsencrypt在将localhost解析为 我的

    server: 'staging'
    

    greenlock express配置的一部分用于。

    2 回复  |  直到 8 年前
        1
  •  4
  •   user844705 user844705    8 年前

    是的,我想你可能有。Letsencrypt为域和子域提供免费的、短期的、经过验证的SSL证书(我认为最多10个)。您试图做的是让LetsEncrypt服务器在以下位置向您的应用程序发出请求: http://localhost/.well-known/acme-challenge http://localhost 到您的服务器。您需要在服务器上运行此代码和模块,在该服务器上可以从您拥有的域上的公共internet访问它。然后LetsEncrypt将能够访问您的应用程序,验证域,并颁发证书。然后,您的站点将在SSL下运行。否则我就搞错了!

        2
  •  2
  •   coolaj86    7 年前

    没有本地主机

    您不能将localhost与Greenlock或任何“让我们加密客户端”一起使用。

    生产阶段

    staging 选择面向公众的生产环境。您可以使用测试域,例如 test.mysite.com www.test.mysite.com

    https://git.coolaj86.com/coolaj86/greenlock-express.js

    推荐文章