2016-03-31 10 views
9

Sto provando ad avviare un server https node.js.node.js https server che non sta rispondendo

ho iniziato con la creazione di un certificato e una chiave seguendo questa guida:

http://gaboesquivel.com/blog/2014/nodejs-https-and-ssl-certificate-for-development/

e li ho messo nella mia directory /app_name/security/keys.

per iniziare il mio server HTTPS, ho il seguente:

const https   = require('https'), 
     fs   = require('fs'); 

if(app.get('env') === 'development') { 

    console.log('dev env!!'); //prints correctly 
    console.log('port: ' + port); //prints correctly 
    const options = { 
     key: fs.readFileSync('./security/keys/key.pem'), 
     cert: fs.readFileSync('./security/keys/cert.pem') 
    }; 

    https.createServer(options, (req, res) => { 

     console.log('https good to go'); //this does not print out anything 

    }).listen(port); 

} 

Quando vado al https://localhost:3000, la pagina genera un errore

This site can’t be reached 

localhost unexpectedly closed the connection. 
ERR_CONNECTION_CLOSED 

Ma non c'è nessun errore sulla console lato server. Inoltre, se vado al normale localhost:3000, ottengo:

The localhost page isn’t working 

localhost didn’t send any data. 
ERR_EMPTY_RESPONSE 

Qualcuno può aiutarmi?

Grazie in anticipo!

---- ---- UPDATE

Io corro sulla porta 443 ora. Inizialmente ho ottenuto un errore:

Error: listen EACCES 0.0.0.0:443 così mi sono imbattuto:

sudo NODE_ENV=development nodemon app

Che non gettare eventuali errori. Tuttavia, quando sono andato a https://localhost:443, ottengo:

This site can’t be reached 

localhost unexpectedly closed the connection. 
+0

credo che potrebbe essere correlato con il porto, https funziona abitualmente su 443 porte. Puoi provare. –

+0

se non viene fornita alcuna porta, funziona su 443. È stata definita una costante di porta. – tanaydin

+0

Gotcha - Ho provato a funzionare su 443 ma sto ancora ottenendo "questo sito non può essere raggiunto". Ho aggiornato la mia domanda con altri dettagli sull'errore. –

risposta

-1

tanti problemi con il vostro codice.

L'ho provato molto rapidamente.

la parola chiave app e port non è definita, righe 4 e 7 rispettivamente.

Ciò causerà un errore di sintassi, impedendo al codice di continuare ulteriormente, pertanto il server non si avvia affatto.

Come ho detto nel mio commento, utilizzare DevTool per eseguire il debug e utilizzare la seguente riga su una CLI devtool server.js -w dove i -w orologi per modifiche apportate ai file e ricarica il server al volo, mentre lo sviluppo. assumendo anche che tu abbia nominato il tuo file di ingresso server.js

0

Ho usato express come server web. installare express:

npm install express --save 

ho preso il codice, ha aggiunto l'utilizzo in express, ha generato il certificato utilizzando openssl, ed eseguito - tutto sembrava buono, il server è stato alto, in ascolto sulla porta 3000 su https.

Il mio codice (che si basa sul vostro codice ...):

var app = require('express')(); 
const https   = require('https'), 
     fs   = require('fs'), 
     port   = 3000; 

if(app.get('env') === 'development') { 

    console.log('dev env!!'); //prints correctly 
    console.log('port: ' + port); //prints correctly 
    const options = { 
     key: fs.readFileSync('/tmp/private.key'), 
     cert: fs.readFileSync('/tmp/publickey.crt') 
    }; 

    https.createServer(options, (req, res) => { 

     console.log('https good to go'); //this does message appears!!! ^_^ 

    }).listen(port); 

} 

Si prega di prestare attenzione al modo in cui ho definito app: var app = require('express')();

È possibile dividere questa definizione in due la linea se è più leggibile:

var express = require('express'); 
var app = express();