1.0.4 • Published 5 months ago

min-route v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

min-route

npm npm bundle size install size

Description

A simple and lightweight library to manage server side routes. It uses only 4 routes for whole project. It is based on Rest API and it is not a replacement of GraphQL but similar to it

Install

$ npm i min-route

Basic Usage

Server index file

const express = require('express');
const minRoute = require('min-route')

const app = express();

minRoute.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 = controller001 **/

//require all the libraries below @controller line
const someLibrary = require('some-library');


const getUser = async (req, res) => {
  //your logic here
    return res.status(200).send({message: "Message"});
}

const postUser = async (req, res) => {
  //your logic here
    return res.status(200).send({message: "Message"});

}

module.exports = {
    getUser,
    postUser
}

Function call from client side

Here- \ 3000 is the server's port number \ controller001 is name of controller file which we defined in server's controller file \ getUser is the function defined in the controller file \ {"param1":"value1", "param2": "value2"} is the object of paramters

Note -

  1. seperate controller name and function name with a dot (.)
  2. parameters should be in an object
let baseUrl = "http://localhost:3000/";
let config = {
	serverFunction:"controller001.getUser/",
  //seperate the controller name and the function name with a dot
	params: {"param1":"value1", "param2": "value2"},
  //parameters should be in an object
	method: "GET"
  }
let reqOptions = {
  url: config.baseUrl + config.serverFunction + config.params,
  method: config.method
  headers: headersList,
  data: bodyContent,
}

axios.request(reqOptions).then(function (response) {
  console.log(response.data);
})

Shorthand

axios.request(url: "http://localhost:3000/controller001.getUser/{"param1":"value1", "param2": "value2"}",
  method: "GET"
  headers: headersList,
  data: bodyContent,).then(function (response) {
  console.log(response.data);
})

Advanced Usage

Custom Middleware

From Client side send middleware array in the parameter object like this-

Note -

  1. Parameter name should be middleware
  2. seperate controller name and function name with a dot (.)
let baseUrl = "http://localhost:3000/";
let config = {
	serverFunction:"controller001.postUser/",
  //seperate the controller name and the function name with a dot
	params: {"param1":"value1", "param2": "value2", "middleware": ["controller002.multerFunction", "controller003.authenticateFunction"]},
  //add a middleware key and pass the middleware functions in an array.
  //seperate the controller name and function name with a dot.
	method: "POST"
  }
let reqOptions = {
  url: config.baseUrl + config.serverFunction + config.params,
  method: config.method
  headers: headersList,
  data: bodyContent,
}

axios.request(reqOptions).then(function (response) {
  console.log(response.data);
})

Shorthand

axios.request(url: "http://localhost:3000/controller001.postUser/{"param1":"value1", "param2": "value2", "middleware": ["controller002.multerFunction", "controller003.authenticateFunction"]}",
  method: "POST", // or GET/PUT/DELETE
  headers: headersList,
  data: bodyContent,).then(function (response) {
  console.log(response.data);
})

License

MIT

1.0.4

5 months ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago