index.js 2.29 KB
const http2 = require('http2');
var fs = require('fs');
const api  = require('./api');
const Koa = require('koa');
const Router = require("koa-router");
const jwtMiddleware = require('./lib/jwtMiddleware');
require('dotenv').config();
const mongoose = require('mongoose');
const bodyParser = require('koa-bodyparser');
const port = process.env.PORT || 3000
//443
const passport = require('koa-passport')
const app = new Koa();
const router = new Router()
const render = require('koa-ejs');
const path = require('path');

const send = require('koa-send');
var options = {
  key: fs.readFileSync('./server.key'),
  cert: fs.readFileSync('./server.crt'),
  allowHTTP1: true
};
//app.use(express.static('images'))  

mongoose.Promise = global.Promise; // Node 의 네이티브 Promise 사용
// mongodb 연결
mongoose.connect(process.env.MONGO_URI).then(
    (response) => {
        console.log('Successfully connected to mongodb');
    }
).catch(e => {
    console.error(e);
});


//app.use(router.routes())
/*
app
  .use(router.routes())
  .use(router.allowedMethods());
  */

  render(app, {
    root: path.join(__dirname, 'views'),
    layout: false,
    viewExt: 'ejs',
    cache: false,
    debug: true
  });
//  app.use(async function (ctx) {
 //   await ctx.render('home/welcome');
 // });
 app
 .use(jwtMiddleware)
 .use(bodyParser()) // bodyParser는 라우터 코드보다 상단에 있어야 합니다.
 .use(router.routes())
 .use(router.allowedMethods())
 .use(passport.initialize());
 
 
 router.get('/', async ctx =>{
  await ctx.render('home/welcome');
});

router.get('/about', async ctx =>{
  await ctx.render('home/about');
});

router.get('/login', async ctx =>{
  await ctx.render('users/index');
});

router.get('/signup', async ctx =>{
  await ctx.render('users/new');
});


  

/*
router.get('/', async (ctx, next) => {
    const rawContent = fs.readFileSync('index.html').toString('utf8')
    ctx.body = rawContent
})*/


//router.use(api.routes());
//app.use(router.routes()).use(router.allowedMethods())
router.use('/api', api.routes());
//app.use(router.routes()).use(router.allowedMethods());


http2
  .createSecureServer(options, app.callback())
  .listen(port, () => console.log("listening on port %i", port));

/*
  app.listen(port, function () {
console.log('server listening on port %d', port);
  });


  */