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:
core.loadRoute(router, 'prefix')
to Router('prefix')
or new AxonRouter('prefix')
import { axonLogger } from "@axonlabs/core";
// or
const { axonLogger } = require("@axonlabs/core");
[!WARNING] @mr-mkz/axon deprecated and transferred to @axonlabs/core
Install Axon.js with npm
npm install @axonlabs/core
You can checkout Axon benchmarks document and results from below link.
More features soon...
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 is stil under constructing and it's not a stable version yet but currently it support this methods:
You can access and create routes with just a few steps.
Router()
function in it. // route prefix is optional
const router = Router('prefix') // easier and newer method
// or
// const router = new AxonRouter('prefix');
router.get(path, controller(req, res))
loadRoute()
function;
const core = Axon(); // easier and newer method
// or
// const core = new AxonCore();
core.loadRoute(router)
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:
Example:
const controller = async (req, res) => {
return res.status(200).body({
message: "Hello, World"
})
}
middleware is a function which runs before running controller for validations or some actions like this and you can use it in two ways.
router.get('/', controller).middleware(async (req, res, next) => next());
you can also use multiple middlewares for a route by repeating middleware function and middlewares will run in order. core.globalMiddleware(async (req, res, next) => next());
you can also use multiple middlewares in this way by adding middleware functions into an array (suggested method) or repeating this line of code.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.AxonHttpsConfig
: Type of core config option HTTPS.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.HttpMethods
: Type of router http methods.RouterExceptionError
: Type of router exceptions.AxonJs use pino and pino-pretty for it's logger and you can use this instance of logger with importing it from @axonlabs/core
.
Logger configuration options will add to config file as soon as possible.
For more information about the pino logger read official documentation of this library.
import { axonLogger } from "@axonlabs/core";
// or
const { axonLogger } = require("@axonlabs/core");
Plugins must use plugin mode of logger.
Example:
import { axonLogger } from "@axonlabs/core";
axonLogger.plugin("Something to log");
You can config Axon core with creating a file in your project root directory.
axon.config.js
axon.config.ts
axon.config.cjs
axon.config.mjs
If you want to have ide suggestions for core config use AxonConfig type.
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)HTTPS
: object to config server for https. (type: AxonHttpsConfig)listen
method runs your webserver.
If you want to run your server on https, you have to set key and cert file in HTTPS config of core to run https server automatically by core
// put this in config file (axon.config.js .etc)
HTTPS: {
key: fs.readFileSync(path.join("server.key")),
cert: fs.readFileSync(path.join("server.crt"))
}
core.listen()
has some default values
core.listen("0.0.0.0", 80, () => {
console.log("server is running on port 80")
});
Contributions are always welcome!