Skip to content

This tool will help you organize your backend very easily. Also you can create all backend api files with just few entries in scheme!

Notifications You must be signed in to change notification settings

technic404/express-route-creator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Routes Schematic Builder

This tool will help you organize your backend very easily. Also you can create all backend api files with just few entries in scheme!

Example of building backend schematic

Create empty file just for init, for example init.js.

const RouteManager = require("./lib/creator");

new RouteManager().schematicBuilder.create("./routes", {
    user: {
        create: {
            method: "post",
            body: {
                email: { type: "string", },
                login: { type: "string", },
                password: { type: "string", minLength: 8, maxLength: 64 }
            }
        },
        delete: {
            method: "delete",
            body: {
                id: { type: "string" }
            }
        },
        get: {
            method: "get",
            query: {
                id: { type: "string" }
            }
        }
    }
})

That example schematic when run with node init.js will create properly prepared api files:

  • /routes/user/create.js
  • /routes/user/delete.js
  • /routes/user/get.js

Routes registration

To make your routes work we have to register it with just two lines of our library code. For better understanding we provide full code snippet with express package inside.

const express = require("express");
const RouteManager = require("./lib/creator");

const PORT = 3000;
const app = express();

app.listen(PORT, () => {
    console.log(`Server is running on localhost:${PORT}`);
});

const manager = new RouteManager(app);

manager.initializer.init("api", "./routes");

Deeper explanation

Here we put app from const app = express(); inside constructor.

const manager = new RouteManager(app);

To register the routes we use manager.initializer.init().

  • First argument is a prefix that will be before each route, for example https://domain.pl/api/user/create.
  • Second argument is a directory where routes are located.
manager.initializer.init("api", "./routes");

Yeah - that's it, you have all your backend files organized and registered! 🚀

Happy coding 🤗

About

This tool will help you organize your backend very easily. Also you can create all backend api files with just few entries in scheme!

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published