@apnet_vector/flare
v1.5.1
Published
Lightweight wrapper over express, allowing to create applications comfortably
Downloads
15
Readme
APNET Flare 1.5.1 - Middlewares and types
Lightweight wrapper over express, allowing to create application comfortably
Features
- Quick creation of an application with basic features
- BodyParser out of the box
- CookieParser out of the box
- Handy log system
- Easy creation of large APIs
- TypeScript support
Guide to creating an app
Create app
const { FlareApp } = require("@apnet_vector/flare");
// You can specify the name of the application
const app = new FlareApp('Test App');
Result:
APNET Flare <version>
[Flare] Creating Application...
[Flare] Test App successfully created
Run app on port
// the application will run on port 3000
app.run(3000, () => {
// you can bind the custom action on successful application startup
console.log('custom action');
})
Result:
APNET Flare <version>
[Flare] Creating Application...
[Flare] Test App successfully created
[Flare] Application successfully started on 3000
custom action //custom action is started
If you go to localhost:3000 you will see the following message:
Cannot GET /
Create API
First, let's understand the API components in Flare
Endpoints
Endpoints allow you to process requests
const homeEndpoint = endpoint({
//path to the endpoint
path: 'home',
//type of HTTP request
requestType: 'get',
middlewares: [
//your middlewares
],
//Handler function, as in Express
handler: ({
//Req, res, next - provided by express
req,
res,
next,
//Cookies from request (cookie-parser)
cookies,
//Request body (body-parser)
body,
//Log function from Flare
log,
}) => {
//Flare will display from which endpoint the message was sent
log('message');
//Send a response to the user
res.json('hello world!')
}
});
Controllers
Controllers - folders for endpoints, they allow to create convenient and beautifully structured APIs
const rootController = controller(
//path of controller
'/city',
//child endpoints or controllers
[
//Connect our endpoint from the previous example
homeEndpoint,
//Let's create another endpoint as an example
endpoint({
path: 'shop',
requestType: 'get',
handler: ({
res
}) => {
res.json('hello world from shop!')
}
}),
]
)
Setup API to the application
app.setupAPI(rootController)
Now let's check the result
On startup, we see the following messages:
[get] [/city/home] successfully added!
[get] [/city/shop] successfully added!
In browser:
http://localhost:3000/city/home:
"hello world!"
http://localhost:3000/city/shop:
"hello world from shop!"
Advanced
To add additional functionality to Express, you will need to access an instance of the Express application (e.g. use HandleBars Engine or express static)
app.getInstance().use(...something...)