Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const PORT = process.env.PORT || 7000;
- const Koa = require('koa');
- const router = require('koa-router')();
- const MemoryFS = require('memory-fs');
- const Promise = require('bluebird');
- const webpack = require('webpack');
- const app = new Koa();
- const fs = new MemoryFS();
- // webpack compiler. once compiled, client.js
- // will be available at fs.readFileSync('/client-bundle.js')
- const compiler = webpack({
- context: __dirname,
- entry: {
- client: './client'
- },
- output: {
- filename: '[name]-bundle.js',
- path: '/'
- }
- });
- compiler.outputFileSystem = fs;
- // start compiler.watch()
- // p will resolve when the first compile finishes
- // we should wait to start the server until p resolves,
- // but it's more readable not to
- const p = Promise.fromCallback(cb => compiler.watch({}, cb));
- p.tap(() => console.log('Client assets compiled'));
- router.get('/client.js', ctx => {
- ctx.type = 'application/javascript; charset=utf-8';
- ctx.body = fs.readFileSync('/client-bundle.js');
- });
- app.use(router.routes());
- app.listen(PORT);
- console.log('Prototype server listening on', PORT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement