Have you ever noticed re-building masters functionality every time in your new application? Yes, we also noticed it. That’s why we build @knovator/masters-node and @knovator/masters-admin so that you don’t have to build them from scratch.
The masters’ package is the combination of two packages, @knovator/masters-node for the Backend and @knovator/masters-react for the Frontend.
@knovator/masters-node
Setup
Masters node adds master routes to your NodeJS application. To create a NodeJS application follow the below steps
mkdir node-app
cd node-app
yarn init -y
yarn add express mongoose @knovator/masters-node
We have the application setup ready, let’s create one src/db.js file that will connect to the database,
// src/db.js
const mongoose = require('mongoose');
mongoose
.connect('mongodb://localhost:27017/knovator')
.then(() => console.info('Database connected'))
.catch((err) => {
console.error('DB Error', err);
});
module.exports = mongoose;
@knovator/masters-admin package provides a facility to upload an image for submaster, so let’s create a route for it.
// src/fileRoute.js
const express = require('express');
const router = express.Router();
router.post(`/files/upload`, (req, res) => {
// TO DO: some file storage operation
let uri = "/image.jpg";
let id = "62c54b15524b6b59d2313c02";
res.json({
code: 'SUCCESS',
data: { id, uri },
message: 'File uploaded successfully'
});
});
router.delete(`/files/remove/:id`, (req, res) => {
// TO DO: some file remove operation
res.json({
code: 'SUCCESS',
data: {},
message: 'File removed successfully'
})
})
module.exports = router;
Now let’s create an app.js file that starts our application,
require('./src/db');
const cors = require('cors');
const express = require("express");
const fileRoutes = require('./fileRoute.js');
const PORT = 8080;
const app = express();
app.use(cors());
app.use(express.static("public"));
app.use(fileRoutes);
// ...
app.listen(PORT, () => {
console.log(`App started on ${PORT}`);
});
Also Read : Image Resizing with Knovator
Usage
Are you ready with the setup? Great, using the masters-node package is very easy to use. Just add masters to app.use.
const { masters } = require('masters-node');
app.use("/admin/masters", masters());
app.listen(PORT, () => {
console.log(`App started on ${PORT}`);
});
Do you want customization we got your back. We can customize the following parameters to it.
authentication– Provides the ability to add authentication to routeslogger– Provides the ability to add logging for Database and Validation, default isconsolecatchAsync– Wraps functions to handle async errorsconvertToTz– Provides the ability to convert dates to other timezones
For Example,
app.use("/admin/masters", masters({
authentication: (req, res, next) => {...},
logger: console,
catchAsync: (function) => (req, res, next) => {...},
convertToTz: ({ tz, date }) => {...}
}));
If you want to use the Master modal somewhere else in your Application. For example, Give a relationship between the Master and User, you can import Master model and use it.
import { Master } from '@knovator/masters-node';
Found useful to you? Please give it a try and feel free to contact us if found any issues.






