├── .babelrc ├── .gitignore ├── license.md ├── package.json ├── readme.md ├── src ├── index.js ├── pokemondb.js └── stats.js └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["transform-async-to-generator"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | build 4 | -------------------------------------------------------------------------------- /license.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Siddharth Jain 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "koa-webpack-boilerplate", 3 | "version": "1.0.0", 4 | "description": "Minimal boilerplate to create REST apps with Node6+, Koa2 and Webpack2. Supports Hot Module Replacement for seamless workflow.", 5 | "keywords": [ 6 | "koa", 7 | "webpack", 8 | "server", 9 | "backend", 10 | "hot module replacement", 11 | "hmr", 12 | "async", 13 | "boilerplate", 14 | "es6" 15 | ], 16 | "scripts": { 17 | "clean": "rm -rf ./build", 18 | "build:prod": "npm run clean && `npm bin`/webpack --env.prod", 19 | "watch": "npm run clean && `npm bin`/webpack --env.dev --watch --verbose --hot", 20 | "start": "node ./build/server.js" 21 | }, 22 | "author": "Siddharth Jain ", 23 | "repository": { 24 | "type": "git", 25 | "url": "https://github.com/sidjain26/koa-webpack-boilerplate" 26 | }, 27 | "license": "MIT", 28 | "devDependencies": { 29 | "babel-core": "^6.10.4", 30 | "babel-loader": "^6.2.4", 31 | "babel-plugin-transform-async-to-generator": "^6.8.0", 32 | "babili-webpack-plugin": "0.0.2", 33 | "webpack": "^2.1.0-beta.14" 34 | }, 35 | "dependencies": { 36 | "koa": "^2.0.0", 37 | "koa-logger": "^2.0.0", 38 | "koa-route": "^3.1.0", 39 | "pokedex-promise-v2": "^2.1.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ### koa-webpack-boilerplate 2 | 3 | Minimal boilerplate to create REST apps with: 4 | * [Node 6+](https://nodejs.org/en/) (Supports 96% of ES6) 5 | * [Koa 2](https://github.com/koajs/koa/tree/v2.x) (async/await transpiled with Babel) 6 | * [Webpack 2](https://webpack.github.io/) (ES6 modules and HMR) 7 | * Hot Module Replacement for seamless workflow. 8 | 9 | --- 10 | 11 | ### How to use 12 | 13 | Read [this](https://goo.gl/11q5fR) piece on Medium. 14 | 15 | ### License 16 | 17 | MIT 18 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import get from './pokemondb'; 2 | import stats from './stats'; 3 | import Koa from 'koa'; 4 | import route from 'koa-route'; 5 | import logger from 'koa-logger'; 6 | 7 | if (module.hot) { 8 | module.hot.accept('./stats', () => {}); 9 | } 10 | 11 | const getPokemonFromAPI = async (ctx, name) => { 12 | try { 13 | const data = await get(name); 14 | ctx.body = stats(data); 15 | } catch (err) { 16 | ctx.throw(404, err.error.detail); 17 | } 18 | }; 19 | 20 | const catchemAll = async (ctx) => { 21 | ctx.body = 'Gotta catch \'em all!'; 22 | } /* Route handler at '/' */ 23 | 24 | const app = new Koa(); 25 | app.use(logger()) 26 | .use(route.get('/', catchemAll)) /* listening on '/' */ 27 | .use(route.get('/:name', getPokemonFromAPI)) 28 | .listen(8000); 29 | console.log('Listening on Port 8000'); 30 | -------------------------------------------------------------------------------- /src/pokemondb.js: -------------------------------------------------------------------------------- 1 | import Pokedex from 'pokedex-promise-v2'; 2 | 3 | const P = new Pokedex(); 4 | 5 | export default function get (pokemon) { 6 | return P.getPokemonByName(pokemon); 7 | }; 8 | -------------------------------------------------------------------------------- /src/stats.js: -------------------------------------------------------------------------------- 1 | export default function stats (data) { 2 | return ( 3 | ` 4 | NAME : ${data.name} 5 | HEIGHT : ${data.height} 6 | WEIGHT : ${data.weight} 7 | BASE XP: ${data.base_experience} 8 | ` 9 | ); 10 | } 11 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path'); 2 | const { dependencies } = require('./package.json'); 3 | const BabiliPlugin = require("babili-webpack-plugin"); 4 | 5 | const nodeModules = {}; 6 | 7 | Object 8 | .keys(dependencies) 9 | .forEach((mod) => { 10 | nodeModules[mod] = `commonjs ${mod}`; 11 | }); 12 | 13 | module.exports = (env = { dev: true }) => ({ 14 | context: resolve(__dirname, './src'), 15 | entry: { 16 | server: env.prod ? './index.js' : ['webpack/hot/poll?1000', './index.js'] 17 | }, 18 | target: 'node', 19 | output: { 20 | filename: '[name].js', 21 | path: resolve(__dirname, './build'), 22 | pathInfo: !env.prod 23 | }, 24 | devtool: env.prod ? 'source-map' : 'eval', 25 | module: { 26 | loaders: [ 27 | { 28 | test: /\.js$/, 29 | exclude: /node_modules/, 30 | loaders: [ 31 | 'babel-loader' 32 | ] 33 | } 34 | ] 35 | }, 36 | plugins: env.prod ? [ 37 | new BabiliPlugin() 38 | ] : [], 39 | externals: nodeModules 40 | }); 41 | --------------------------------------------------------------------------------