Skip to main content

Installation

To get started with file-express-router, you need to install it as a dependency in your Node.js project. Follow these steps:

Step 1: Install the Package

You can install File Express Router using npm or yarn:

// Using npm
npm install file-express-router

// Using yarn
yarn add file-express-router

Step 2: Install peer dependencies

File Express Router requires Express.js as a peer dependency. If you don’t already have Express installed, add it to your project:

// Using npm
npm install express

// Using yarn
yarn add express

Step 3: Verify Installation

After installation, you can verify that the package is installed correctly by checking your package.json file. It should include file-express-router and express in the dependencies section.

{
"dependencies": {
"express": "^4.17.1",
"file-express-router": "^1.0.0"
}
}

That’s it! You have successfully installed File Express Router in your Node.js project. You can now start using it to create routes based on file structure.

Step 4: Set Up Your Project

Once installed, you’re ready to set up your project. Here’s a minimal example to get started:

  1. Create a new directory for your project (if you haven’t already):
mkdir my-express-app
cd my-express-app
  1. Initialize a new Node.js project (if you haven’t already):
npm init -y
  1. Create the following folder structure:
my-express-app/
├── routes/
│ ├── status.ts
│ └── users/
│ └── [id].ts
├── app.ts
└── package.json
  1. Add the following code to app.ts:
import express from 'express';
import { Router } from 'file-express-router';

const startServer = async () => {
const app = express();

const routesDir = `${__dirname}/routes`;
const router = await Router({ dir: routesDir });
app.use('/api', router);

app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
};

startServer();
  1. Add a simple route in routes/status.ts:
import { RequestHandler } from 'express';

export const get: RequestHandler = (req, res) => {
res.json({ message: 'Ping Pong!' });
};
  1. Add a dynamic route in routes/users/[id].ts:
import { RequestHandler } from 'express';

export const get: RequestHandler = (req, res) => {
res.json({ userId: req.params.id });
};
  1. Start your server:
npx ts-node app.ts
  1. Test your routes: