Skip to content

ManuUseGitHub/mz-express-autoroute-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maze-Autoroute 🛣

Mapping your routes by a folder structure.

This document serves as a briefing for the npm package I published here. It provides you with enough documentation to help set up a demo showcasing how the maze-autoroute works. However, all the detailed technical information is available in on NPM.

Go check the documentation to complete this one with more details.

Don't waste time anymore on creating new routes!

Without an Autoroute

On a bad day, with the need of creating a new project with routing you have to do following steps :

  1. Create your server minimal code.
  2. Create your variable to store the new route.
  3. Import the custom route module.
  4. Edit the server file in the right place to make the new route available.
  5. Create the module Folder and its index.js exporting a router.
  6. Define your route(s) into the new index.js module.
  7. Test if everything is working (you may have made some mistakes in the previous steps - like typos).
  8. Make potential corrections.
  9. Breath.

With an Autoroute

Still on a bad day, on a rush, tired, or whatever :

  1. Create your server minimal code.
  2. Create a Folder tree containing your modules exporting a router.
  3. Breath.

Getting started

You will clone this already made project so you can have a feel to it in no effort or so.

Open a terminal in an empty folder, and clone the repo :

$ git clone https://github.com/ManuUseGitHub/mz-express-autoroute-demo.git .

NOTE: White the dot "." at the end of the command, the content of the repository will be copied and not the containing folder.

Then install dependencies

$ npm i

Then run the server in watch mode.

$ npm run watch-server

This command will rerun the server each time you change the server or an endpoint. Via this command you can notice that you can create/delete/rename(folder of) endpoints on the fly and the server will adapt! Magic!

OR if you want it to stay calm, just run this command:

$ npm run server

Time to play!

  1. Delete eveything that is in /backend/routes.
  2. Create an index.js file. Then copy this code into it.
const express = require("express");
const router = express.Router();

router.get('/', async (req, res) => {
    res.send(`Welcome to this resource at FOLDER ${__dirname.replace(process.cwd(),"")}`);
})

router.get('/greet/name/:something', async (req, res) => {
    res.send(`Please to meet you ${req.params.something} ! I'm an endpoint defined at FOLDER ${__dirname.replace(process.cwd(),"")}`);
})

module.exports = router;
  1. run the server if it is not already running in watch mode
$ npm run watch-server
# OR
$ npm run server
  1. Open your browser at localhost:4000

Here is what you can see

Alt text

  1. Copy the same file, but the copy into a new folder /backend/routes/Feelgood , save and once done (rerun the server if stopped), go to the browser at corresponding address (localhost:4000/Feelgood). You should see this :

Alt text.

  1. Enjoy !

Options

You can find the options in the server.js. There is a preset. Just check these lines of code.

// ROUTES ----------------------------------------------------------
const onmatch = ({ route, module }) => app.use(route, require(module));

const options = {
    onmatch,
    verbose: true,
    rootp: "/backend/routes",
    subr: "b64",
    translations: [{ from: "helloworld/hope/word", to: "worldhello" }],
    flat: true
}

const autoroute = new Autoroute();
autoroute.getMapping(options);
// END ROUTES ------------------------------------------------------

To better understand how you can tune the autorouter, take a look at the documentation 😀 ! It is available on NPM : maze-autoroute and on GitHub : maze-autoroute.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published