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.
On a bad day, with the need of creating a new project with routing you have to do following steps :
- Create your server minimal code.
- Create your variable to store the new route.
- Import the custom route module.
- Edit the server file in the right place to make the new route available.
- Create the module Folder and its index.js exporting a router.
- Define your route(s) into the new index.js module.
- Test if everything is working (you may have made some mistakes in the previous steps - like typos).
- Make potential corrections.
- Breath.
Still on a bad day, on a rush, tired, or whatever :
- Create your server minimal code.
- Create a Folder tree containing your modules exporting a router.
- Breath.
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
- Delete eveything that is in
/backend/routes
. - 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;
- run the server if it is not already running in watch mode
$ npm run watch-server
# OR
$ npm run server
- Open your browser at localhost:4000
Here is what you can see
- 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 :
- Enjoy !
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.