-
Notifications
You must be signed in to change notification settings - Fork 2
Routes & Routers
To start, create a new typescript file in ./src/routes
. For this example we will be using myRouter.ts
.
Then import the Router
function from express and initialize the router:
import { Router } from 'express'
const MyRouter = Router()
Now we can choose to export this router in two ways:
- Use DRL w/ object metadata
- Export without DRL
Note: DRL stands for "Dynamic Route Loading"
First we'll start off with the two more standard methods using DRL.
To export with metadata you simply define a property on MyRouter
, and export it as default:
const MyRouter = server.router("/myRouter")
export default MyRouter
This will hook the router you created to /myRouter
.
Note: Routers can be exported as router
rather than default:
export const router = MyRouter
Alternatively you can export directly to ./src/index.ts
without using DRL.
Note: This method is not recommended and is here purely for reference. You will most likely be asked to change to using DRL if you submit a pull request with it.
./src/routes/myRouter.ts
:
const MyRouter = Router()
export default MyRouter
./src/routes/index.ts
:
import MyRouter from './myRouter'
export {
MyRouter
}
./src/index.ts
:
import {
MyRouter
} from './routes/index'
...
app.use("/myRouter", MyRouter)