Last active
June 9, 2019 21:27
-
-
Save jrgleason/f9a6dba2a3784e8b504a7623085ffa88 to your computer and use it in GitHub Desktop.
Trying to get Koa-pug working
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http2 from "http2"; | |
import fs from "fs"; | |
import { promisify } from "util"; | |
import Koa from "koa"; | |
import Pug from "koa-pug" | |
import Router from "koa-router"; | |
import serve from "koa-static"; | |
import mount from "koa-mount"; | |
const PORT = process.env.PORT || "3000"; | |
const CERT_FILE = process.env.CERT_FILE | |
const KEY_FILE = process.env.KEY_FILE | |
export class Application{ | |
constructor(){ | |
this.app = new Koa(); | |
this.hosted = Application.getHostedApp(); | |
this.vueApp = Application.getVueApp(); | |
this.uiApp = Application.getUIApp(); | |
this.app.use(mount(this.hosted)); | |
this.app.use(mount("/vue", this.vueApp)); | |
this.app.use(mount("/ui", this.uiApp)); | |
this.options = { | |
key: fs.readFileSync(KEY_FILE), | |
cert: fs.readFileSync(CERT_FILE) | |
} | |
this.server = http2.createSecureServer(this.options, this.app.callback()); | |
this.server.on("error", (err) => console.error(err)); | |
} | |
static getUIApp(){ | |
const app = new Koa(); | |
app.use(serve("./node_modules/@jrg/main-ui-private/dist")); | |
return app; | |
} | |
static getVueApp(){ | |
const app = new Koa(); | |
app.use(serve("./node_modules/vue/dist")); | |
return app; | |
} | |
// TODO: Make own class | |
static getHostedApp(){ | |
const app = new Koa(); | |
const pug = new Pug({ | |
viewPath: './src/views' | |
}); | |
pug.use(app); | |
const router = new Router(); | |
router.get("/", function(ctx){ | |
ctx.render('index'); | |
}) | |
router.get("/test",function(ctx){ | |
ctx.body = "Test" | |
}); | |
app.use(router.routes()); | |
app.use(router.allowedMethods()); | |
return app; | |
} | |
start(){ | |
const listenAsync = promisify(this.server.listen.bind( this.server )); | |
return listenAsync(PORT); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http2 from "http2"; | |
import fs from "fs"; | |
import { promisify } from "util"; | |
import Koa from "koa"; | |
import Pug from "koa-pug" | |
import Router from "koa-router"; | |
import serve from "koa-static"; | |
import mount from "koa-mount"; | |
const PORT = process.env.PORT || "3000"; | |
const CERT_FILE = process.env.CERT_FILE // TODO: Not working || "~/Code/certs/jackiergleason.cert" | |
const KEY_FILE = process.env.KEY_FILE // TODO: Not working || "~/Code/certs/jackiergleason.key" | |
export class Application{ | |
constructor(){ | |
this.hosted = Application.getHostedApp(); | |
this.options = { | |
key: fs.readFileSync(KEY_FILE), | |
cert: fs.readFileSync(CERT_FILE) | |
} | |
this.server = http2.createSecureServer(this.options, this.hosted.callback()); | |
this.server.on("error", (err) => console.error(err)); | |
} | |
// TODO: Make own class | |
static getHostedApp(){ | |
const app = new Koa(); | |
const pug = new Pug({ | |
viewPath: './src/views' | |
}); | |
pug.use(app); | |
const router = new Router(); | |
router.get("/", function(ctx){ | |
ctx.render('index'); | |
}) | |
router.get("/test",function(ctx){ | |
ctx.body = "Test" | |
}); | |
app.use(router.routes()); | |
app.use(router.allowedMethods()); | |
return app; | |
} | |
start(){ | |
const listenAsync = promisify(this.server.listen.bind( this.server )); | |
return listenAsync(PORT); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment