나를 위한 기록들

NodeJS https 설정, SSL 본문

JS/Node JS

NodeJS https 설정, SSL

lyasee 2017. 9. 12. 20:47

// 소스코드


const express = require('express');

const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const http = require('http');
const https = require('https');
const fs = require('fs');
const index = require('./routes/index');
const users = require('./routes/users');


const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(bodyParser.urlencoded({extended: false}));


app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


app.use('/', index);
app.use('/users', users);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});


// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};


// render the error page
res.status(err.status || 500);
res.render('error');
});


app.get('*', cors(), function(req, res, next){
res.sendfile(path.join(__dirname, 'public/index.html'));
});


const options = {
key: fs.readFileSync('경로~/key.pem'),
cert: fs.readFileSync('경로~/cert.pem')
};


const port1 = 80; // http 포트
const port2 = 443; // https 포트


http.createServer(app).listen(port1, function(){
console.log("Http server listening on port " + port1);
});


https.createServer(options, app).listen(port2, function(){
console.log("Https server listening on port " + port2);
});


module.exports = app;




Comments