Features
- 🔧 Zero configuration needed to get started.
- 🔥 hot module replacement on your controllers
- 🍾
async
/await
support - ⚙️ Use as middleware with
express
or standalone.
TOC
Setup
Installation
CLI
npm install -g hot-controller
or
yarn global add hot-controller
Locally in project
npm install hot-controller --save
oryarn add hot-controller
- in your
package.json
add the following:"scripts":..."start": "hot-controller serve""dev": "hot-controller"
express
application
With existing -
npm install hot-controller --save
oryarn add hot-controller
-
Add the middleware to existing
express
:const hotControllers = ;...app;
Controllers
Create a directory in the root of your project called controllers
All files in this directory will be parsed as a controller with hot-controller
.
Example
/controllers/home.js
; // which path it should reside on@ // declare your methods below @Route // matches /api { // res; } @Route { // redirect user to somewhere else res; } @Route // matches /api/users/:id async { // this is a async method const user = await ; // get the user data // send it as json res; }
Get more examples in our examples repo
Plugins
hot-controller
fully supports plugins and is easily activatable in your config.
We follow the "babel plugin naming scheme". (hot-controller-plugin-[PLUGIN_NAME]
) and later in your .controllersrc
(or what configuration method you're using) add "plugins": [ "plugin-name" ]
Typescript
To write your controllers in typescript
please checkout our very own plugin [hot-controller-plugin-typescript)(https://github.com/hot-controller/hot-controller-plugin-typescript)
Write your own plugin
Writing a plugin for hot-controller
is very simple. All it takes is a function.
module { events; events; events;};
Hooks
Your controllers can be even more controlled via hooks. Read more below for before and after
Before
@ async { // check acl, push to logs or whatever needed to do before we continue the request ; // dont forget this if you want to continue the request. }
After
@ { // nothing more to do, // request already sent to client. but maybe you want to log something. }
Configuration
There are many ways to configure hot-controller:
-
Create a
.controllersrc.json
- Support for
.json
,.json5
,.yaml/.yml
(just append extension to.controllersrc
, ex:.controllersrc.yml
)
- Support for
-
Add
controllers
section to yourpackage.json
file.
Options
/** * where is your controllers? * type: string * default: ./controllers */ "dir": "" /** * sets the root path for controllers (example: /api) * type: string * default: / */ "path": "/api" /** * use plugins * type: string[], func[] */ "plugins":
Examples
-
via
.controllersrc.json
-
in
package.json
..."controllers":"dir": "./api"...
Custom Babel Config
hot-controller
allows for custom .babelrc
for your controllers. This file is optional.
In order to extend our usage of babel, you can simply define a .babelrc
file at the root of your app or in your controllers
directory.
Contributors
Thanks goes to these wonderful people (emoji key):
Philip Oliver 💻 📖 🤔 👀 ⚠️ 🔧 |
Viktor S 📖 💻 |
an90dr 📖 |
Umang G. Patel 📖 |
---|
This project follows the all-contributors specification. Contributions of any kind welcome!