2017-10-14 2 views
0

localhost에 HTTPS 서버를 설정하려고했지만 서버가 실행 중일 때 html 파일을 렌더링하는 방법을 모르겠습니다. ') 함수되지 .render (', 대안은 무엇인가노드 Js가있는 HTTPS 렌더링 html 파일

var https = require('https'); var fs = require('fs'); 

var options = { key: fs.readFileSync('client-key.pem'), 
       cert: fs.readFileSync('client-cert.pem') }; 

var a = https.createServer(options, function (req, res) { 
     console.log('Server is starting');   
     res.writeHead(200);            
    // res.end("hello world\n"); 
     res.render('index.html'); 

}).listen(8000); 

내가 로컬 호스트에 액세스 할 수 있습니다,하지만 난 HTML 파일을 렌더링하려 할 때마다, 나는의 오류 메시지를 받았습니다 : 다음은 내 코드입니다 서버가 실행 중일 때 html 파일을 '호출'합니다. 어떤 도움을 주셔서 감사합니다!

답변

1

파일 시스템을 사용해야합니다. 준비 파일, content 내부를 저장하고 클라이언트

var fs = require('fs'); 

fs.readFile('./index.html', function (error, content) { 
    if (error) { 
     response.writeHead(500); 
     response.end('Error'); 
    } else { 
     response.writeHead(200, { 'Content-Type': 'text/html' }); 
     response.end(content, 'utf-8'); 
    } 
}); 

의 전체 HTML 파일을 보내드립니다 이 방법 당신은 순수 nodejs에서 render 기능이 없습니다. 그것을 사용하고 싶다면 expressj를 사용하도록하십시오.

내가 작성하는 코드는 createServer 기능

+0

안녕하세요 내부에,이 코드는 정말 감사합니다, 저를 위해 일을해야합니다! 감사!! – JackPowell