使用https快速路线

时间:2018-05-15 19:45:17

标签: node.js express https

我想制作https express服务器,我已经遇到了问题。服务器运行没有错误。对服务器的所有http请求都被路由到https,所以它也没问题。但是我有以下问题:

  1. 当我尝试连接到加法器http://localhost" Home"应该显示,但我得到Access to localhost was denied You don't have authorization to view this page. HTTP ERROR 403
  2. 如果我尝试连接此https://localhost:8080,我可以访问任何路由,但在这种情况下,我需要指定端口8080,如果我希望服务器像这样监听默认的https端口443 https.createServer( options, app ).listen( 443 );所以我致电https://localhost,我得到同样的错误Access to localhost was denied You don't have authorization to view this page. HTTP ERROR 403
  3. 我的服务器代码:

    const https = require( "https" ),
        fs = require( "fs" ),
        express = require( 'express' );
    
    var key = fs.readFileSync( 'config/server.key' );
    var cert = fs.readFileSync( 'config/server.crt' );
    var ca = fs.readFileSync( 'config/server.crt' )
    
    var options = {
        key: key,
        cert: cert,
        ca: ca
    };
    
    const app = express();
    
    app.get( '/hello', ( req, res ) => {
        res.send( "Hello" )
    } )
    
    app.get( '/', ( req, res ) => {
        res.send( "home" )
    } )
    
    app.listen( 80 );
    
    https.createServer( options, app ).listen( 8080 );
    

0 个答案:

没有答案