@mr-mkz/axon
v0.4.3
Published
A backend library that aims to be simple and powerful.
Downloads
408
Maintainers
Readme
Axon.js
like the brain's powerful neural pathways, simple yet strong.
Axon is a backend library who tries to be simple and powerfull.
Currently Axon is 2X faster than Express. :D please checkout Axon Benchmarks
Latest change:
- fixed core freeze when load route function didn't bind. #3
- Plugin manager system added to core. (Document will update soon - 2024/10/24)
Installation
Install Axon.js with npm
npm install @mr-mkz/axon
Benchmarks
You can checkout Axon benchmarks document and results from below link.
Badges
Features
- Simple routing system
- Support methods: GET, POST, PUT, PATCH, DELETE, OPTIONS.
- Flexible routing system. (You can define routes in another files and then add them to core)
- Default core logger
- Configurable core
- Plugin manager (You can create your own plugins and use them in other projects)
- Controllers and Middlewares
- Default cors configuration method
More features soon...
Roadmap (still thinking)
- Support controllers better than now.
- Some changes in response structure.
- Response meta generator.
- Auto error detector (maybe)
- Default schemas.
- Default database connection methods.
Documentation
Currently Axon has a main core and a router class which you can make instance from router class every where you want and then gave the router instance to core to load routes.
More complete examples:
Router
Router is stil under constructing and it's not a stable version yet but currently it support this methods:
- GET
- POST
- PUT
- PATCH
- DELETE
- OPTIONS
You can access and create routes with just a few steps.
- creating a variable with a optional name and put
Router()
function in it. - define your routes with methods which you want and controller.
const router = Router() router.get(path, controller(req, res))
- load your routes in core with
loadRoute()
function;const core = Axon(); // easier and newer method // or // const core = new AxonCore(); core.loadRoute(router)
- Done, you created your routes successfully :D
Controller
you have to pass your controller to your route, compute and do your jobs in controller and when you want to response to user (each response, error and success) you must return res with some options which example and description for each option is below.
res.{option}
Options:
- status: This option must set to get access to other options. (you can use each option just once)
- message: This option will set the response message for example response message of status 200 is OK.
- setHeader: You can set header with this option and also you can use this option many times.
- body: This option will end request and send response to user so you must use this as last option
Example:
const controller = async (req, res) => {
return res.status(200).body({
message: "Hello, World"
})
}
Middleware
middleware is a function which runs before running controller for validations or some actions like this and you can use it in two ways.
- adding a middleware for special route
- Example:
you can also use multiple middlewares for a route by repeating middleware function and middlewares will run in order.router.get('/', controller).middleware(async (req, res, next) => next());
- Example:
- loading middleware as a global middleware in Axon core.
- Example:
you can also use multiple middlewares in this way by adding middleware functions into an array (suggested method) or repeating this line of code.core.globalMiddleware(async (req, res, next) => next());
- Example:
Types
AxonJs has some types which can help you in developing your applications for auto suggestions of your code editor.
Types detect automatically in Typescript but you need to set types for IDE suggestions in Javascript (Javascript Example).
AxonCoreConfig
: Type of core config object for configuration Axon core as you want.AxonResponseMessage
: Type of core config option RESPONSE_MESSAGES.AxonCorsConfig
: Type of core config option CORS.Request
: Type of controller request param. (IncomingMessage)Response
: Type of controller response param. (ServerResponse)Headers
: Type of response headers. (OutgoingHeaders)nextFn
: Type of next function param in middleware.Controller
: Type of controller function.Middleware
: Type of middleware function.
Axon Core config
you can config Axon core with loadConfig
method.
if you want to have ide suggestions for core config use AxonCoreConfig type.
Usage:
core.loadConfig({
LOGGER: false
})
Configs:
DEBUG
: boolean to set debug mode of core. (default false)LOGGER
: boolean to set core logger on or off. (default true)LOGGER_VERBOSE
: boolean to set core logger in verbose mode. (default false)RESPONSE_MESSAGES
: object to change default value of some core responses. (type: AxonResponseMessage)CORS
: object to change core cors settings. (type: AxonCorsConfig)
Running server
listen
method runs your webserver.
core.listen()
has some default values
- host: default value of host in 127.0.0.1
- port: default value of port is 8000
- callback: if you don't set callback function, core will log running message automatically if logger be on in core config.(logger is on in default config)
core.listen("0.0.0.0", 80, () => {
console.log("server is running on port 80")
});
Contributing
Contributions are always welcome!