Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sib61/express-folder-router
https://github.com/sib61/express-folder-router
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/sib61/express-folder-router
- Owner: SIB61
- License: mit
- Created: 2023-05-14T09:06:41.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-23T19:17:57.000Z (over 1 year ago)
- Last Synced: 2024-05-29T07:56:56.029Z (9 months ago)
- Language: TypeScript
- Size: 374 KB
- Stars: 5
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## express-folder-routing
This is a minimalist package for express to add folder based routing in it.
## Installation
```bash
npm install express-folder-router
```## usage
```js
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:
```js
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:
```js
configureFolderRouter(app, {
extraMethods: ["head", "ws"],
});
```We can turn off the api route logs by:
```js
configureFolderRouter(app, {
log: false,
});
```## create an api endpoint
```js
// routes/hello/index.js or routes/hello.js
// endpoint: localhost:3000/helloexport 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.
```js
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
```js
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](https://github.com/SIB61/express-folder-router/blob/main/imgs/example.png)