Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SIB61/express-folder-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

express-folder-routing

This is a minimalist package for express to add folder based routing in it.

Installation

npm install express-folder-router

usage

import express from "express";
import { configureFolderRouter } from "express-folder-router";

const app = express();
configureFolderRouter(app);

app.listen(3000, () => {
  console.log("listening to port 3000");
});

The default directory for our api routes is 'routes'. If we want to change the default directory name or path we can pass a second optional param:

configureFolderRouter(app, {
  routeDir: "src/routes",
});

If we want to use any extra methods like head or ws etc. we can pass another option as below:

configureFolderRouter(app, {
  extraMethods: ["head", "ws"],
});

We can turn off the api route logs by:

configureFolderRouter(app, {
  log: false,
});

create an api endpoint

// routes/hello/index.js or routes/hello.js
// endpoint: localhost:3000/hello

export const GET = (req, res) => {
  res.send("this is a get request");
};

export const POST = (req, res) => {
  res.send("this is a post request");
};

alternatively export a default function that will receive all requests except the ones that you export as a named function.

export const GET = (req, res) => {
  res.send("This is a get request");
};

// this function will catch all the http methods except GET
export default function (req, res) {
  res.send("Success");
}

Use middleware

export const GET = [authMiddleware,(req,res)=>{
  res.send("get hello")
}]

function authMiddleware(req,res,next){
    if(\*check authentication*\){
        next()
    }else{
        res.status(401).json({message:"unauthenticated"})
    }
}
We can make a dynamic route by simply naming the file as "[dynamicName].js" or ":dynamicName.js".
We can make a catch-all route by simply naming the file as [...name].js or *.js.

alt dashboard

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published