Express control
Simplified Express wrapper API for Node apps.
Typings included.
Basic app
This a basic example of a Node app with Express control:
import { createApp } from "express-control"
async function main() {
const App = createApp()
App.start()
}
main()
By default, your app will run on port 8000. You can pass an object to createApp
specifying the port you want to use:
createApp({ port: 3000 })
Controllers
Controllers handle requests. Routes will be handlend according to how you define your controllers' methods:
import { RestController } from "express-control"
export default RestController({
async "get /"(req, res){
return "hello control"
}
async "get /hello"(req, res){
return {
message:"hello"
}
}
})
Using a controller
To use a controller, add it as a controller to your app:
import { createApp } from "express-control"
import AppController from "./controllers/app"
async function main() {
const App = createApp()
App.controller("/", AppController)
App.start()
}
main()
Now routes for /
will be handled by the AppController:
If a methd name is /hello
in that AppController, it will handle the route /hello
Methods support all the Express features and they should start with the method name they support
async "post /info"(req,res) {
status(500)
return "Error saving info"
}
They also support params
:
async "get /user/:id"(req,res) {
return {
id: req.params.id
}
}
If you want to customise the response, you can return a function instead of an object:
async "get /info"(req, res) {
return () => {
res.status(401).json({
error: "please login"
})
}
}
That's it!
Feel free to contribute :-)
I will include examples for integrations with different frameworks and libraries: Next.js, socket.io, etc.