1.0.12 • Published 2 years ago
@anveeg/mini-route v1.0.12
miniRoute
Install
$ npm i @anveeg/mini-route
Usage
Server index file
const express = require('express');
const MiniRoute = require('@anveeg/mini-route')
const app = express();
MiniRoute.api(app, [<path of controller file 1>, <path of controller file 2>, ...... <path of controller file n>])
app.listen(<port-number>, () => {});
Controller file
add the name of the controller in all your controller file's first line.
Syntax - /** @controller = <name> **/
example
/** @controller = control1 * */
const someLibrary = require('some-library');
const funtion1 = async (req, res) => {
console.log('funtion1')
return res.status(200).send({status: 0, message: "Messages"});
}
const function2 = async (req, res) => {
console.log('function2')
return res.status(200).send({status: 0, message: "Messages"});
}
module.exports = {
funtion1,
function2
}
Function call from client side
Here- \ 3000 is the server's port number \ control1 is name of controller file which we defined in server's controller file \ function 1 is the function defined in the controller file \ {"param1":"value1", "param2": "value2"} is the object of paramters
Note -
- seperate controller name and function name with a dot (.)
- parameters should be in an object
let reqOptions = {
url: "http://localhost:3000/control1.function1/{"param1":"value1", "param2": "value2"}",
method: "GET", // or POST/PUT/DELETE
headers: headersList,
data: bodyContent,
}
axios.request(reqOptions).then(function (response) {
console.log(response.data);
})