this is a demo source code for tech talk 2017/03/22. demostrating the cutting-edge way to do an nodejs server including:
- test-driven
- all es6 features -- mostly async function pattern
- the next version of koa
- middlewares and routers stack
- ORM and mocked data/service
- flow
check each commit for more detail
- setup the first router
- first orm model
- better error handling
- authenticate middleware
- create account. mock objects
- add relationship between models
git v2.10.1
node v7.4.0
cnpm v4.4.2
babel-node v6.18.0
mocha v3.2.0
mysql v5.7.17
direnv v2.10.0
export PORT=3000
export AUTH_SECRET=ilabs-intro
export MYSQL_DB=ilabs
export MYSQL_USERNAME=rexsheng
export MYSQL_PASSWORD=
export MYSQL_HOST=localhost
export MAIL_ACCOUNT=(your account)
export MAIL_PASSWORD=(your password)
git init
npm init
cnpm i -D babel-core babel-loader babel-polyfill babel-preset-es2015 babel-preset-stage-0
cnpm i -D babel-plugin-transform-flow-strip-types babel-plugin-transform-runtime
your .babelrc
should look like:
{
"presets": ["es2015", "stage-0"],
"plugins": [
"transform-runtime",
"transform-flow-strip-types"
]
}
plan to use koa2 for http service, sequelize for ORM, jwt for authenticate, and we do test-driven develop.
cnpm install koa@latest koa-body@next koa-router@next --save
cnpm install boom jsonwebtoken --save
cnpm install sequelize --save
cnpm install chai sqlite3 sequelize-fixtures superagent --save-dev
mysql> create database ilabs;
mysql> grant all on ilabs.* to ''@'localhost';
(start playing)