A mini node server boilerplate on top of Koa, aiming to shed light on how Egg works.
Slightly different on how are the ctx
, app
referenced.
You should take it more like a shadow project for egg, rather than a framework, though, this project is suitable for small backend node project.
-
Static sever supported, the path can be configured in the
config/config.js
, which is relative to the directory/app
. -
Session supported.
-
Using bunyan as logger, however the logger config is hardcoded.
-
Websocket, using ws. You have to set these varibles (
token
for example) in the first connection in the url.// client const wsc = new WebSocket('ws://localhost:8081/path-for-specific-websocket-server-instance?token=lmKam8IMg52dHbyCTk0A&uuid=hoO0TNpcIac0q7iXM139oQ') // server wss.on('connection' , (ws, req) => { ws.on('message', async (message) => { console.log('In thunder receive message: %s', message); console.log('[Thunder] got headers: %o ',req.headers); const { query } = url.parse(req.url, true); console.log('[Thunder] got token "%s", got uuid "%s" ', query.token, query.uuid); ws.send('We[thunder] have you '.concat(message)); ws.close(); }); ws.on('close', async () => { console.log('thunder close'); }); })
-
Write router in
/app/router/
, example. -
Write the corresponding controller in
/app/controller
, example. -
If you need corresponding service, just create one in
/app/service
, example. -
And in case you need some connection to database or redis, I recommend you to write those DAOes in
/app/dao
, and not register them to theapp
orctx
to keep the flexibility.Here, this boilerplate does NOT support plugin extensions, so nevermind.
In my opinion, the plugin extensions is used to extend the
app.context
, granting more functionality, so that we can access to them easily just by refering a property incontext
in the controllers. So if you need more plugin, just following this concept. -
Here is the configuration files for you. As for different production environment, we got config.local.js (optional) and config.prod.js (optional) which will overwrite those config in config.js.
Feel free to fork or clone the project to customize.
The files in /app/loader
help you to register those functions or classes in the app
fields, such as controller
, service
, logger
etc.
In some degree, these
loader
functions play the role of theplugin
orextensions
.
-
Server side rendering Engine.
-
Process deamon such as
pm2
. -
Agent/Worker mode for robust.Check app/index.js for details. -
Plugin extensions.
-
Testing Case.
-
Error Monitor.
-
Event trace.