2.3.0 • Published 1 year ago

express-folder-router v2.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

2.3.0

1 year ago

2.2.1

1 year ago

2.2.0

1 year ago

2.2.3

1 year ago

2.2.2

1 year ago

2.2.5

1 year ago

2.2.4

1 year ago

2.2.7

1 year ago

2.2.6

1 year ago

2.0.0-beta.2

1 year ago

2.0.0-beta.1

1 year ago

2.0.1-beta.0

1 year ago

2.0.0-beta.0

1 year ago

2.0.0-beta.3

1 year ago

2.2.8

1 year ago

1.0.1-beta.2

1 year ago

1.0.0-beta.2

1 year ago

1.0.0-beta.3

1 year ago

1.0.0-beta.4

1 year ago

1.0.0-beta.5

1 year ago

1.0.0-beta.1

1 year ago

1.0.0-beta.6

1 year ago

1.0.0-beta.7

1 year ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.4

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago