Auto Routes Linking is an npm module that helps you automatically create routes for your ExpressJS application based on the file and folder structure in your project. By default, all route files should be located in the "src/routes" folder. However, you can specify a different route path if needed.
To install Auto Routes Linking, run the following command in your terminal:
npm i auto-routes-linking
Auto Routes Linking is a Node.js module that helps create routes automatically in a express.js app. It simplifies the process of creating routes by following the file and folder structure in Next.js.
The default location for route files is the "routes" folder, which is located in the "src" directory. However, this can be changed by specifying a different path when using the npm module.
By default, the prefix for all auto-generated routes is "api". This can be changed by specifying a different prefix when using the npm module.
To create a route, you simply need to create an index.js file in the appropriate folder. For example, to create a route for "api/user", you can create an index.js file in the "routes/user" folder and specify the appropriate method:
"get.user" = (request,response)=>{ res.send("get route for api/user") }
Supported methods include GET, POST, PUT, and PATCH.
If you have many routes, it can become unwieldy to put them all in a single index.js file. To address this, you can create a folder for each route and include an index.js file in each folder. For example, to create routes for "api/user" and "api/product", you can create two folders named "user" and "product" in the "routes" folder, each with its own index.js file.
routes -user -index.js -product -index.js
Inside each index.js file, you can specify the appropriate methods for the route. For example:
get = (request,response)=>{ res.send("get route for api/user") }
post = (request,response)=>{ res.send("get route for api/user") } delete = (request,response)=>{ res.send("get route for api/user") }
export = { get, post, delete }
You can also create dynamic routes using the "[param]" syntax. For example, to create a route for "api/user/:user", you can create a folder named "[user]" inside the "routes/user" folder, and include an index.js file inside the "[user]" folder.
routes -user -index.js -[user] -index.js
Inside the index.js file, you can specify the appropriate methods for the dynamic route. For example:
get = (request,response)=>{ res.send("get route for api/user/:user") }
export = { get }
You can then import the module and use it to automatically generate routes:
var express = require('express'); var routeLinking = require('auto-routes-linking'); const app = express()
routeLinking(app)// default prefix is "api" and default route path is "src/routes". To change this, you can specify the prefix and path as arguments: routeLinking(app,"api/v1","/routes") //or routeLinking(app,"api/v1","/anything")
app.listen(3000, function () { console.log('Example app listening on port 3000!'); });