https://github.com/kth/kth-node-express-routing
Define named routes using express routing syntax. Allows exporting route defs javascript object
https://github.com/kth/kth-node-express-routing
Last synced: about 1 year ago
JSON representation
Define named routes using express routing syntax. Allows exporting route defs javascript object
- Host: GitHub
- URL: https://github.com/kth/kth-node-express-routing
- Owner: KTH
- Created: 2017-04-28T12:37:27.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2025-04-09T06:44:01.000Z (about 1 year ago)
- Last Synced: 2025-06-13T19:05:19.423Z (about 1 year ago)
- Language: JavaScript
- Size: 718 KB
- Stars: 0
- Watchers: 17
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# kth-node-express-routing
This is a wrapper for express route to allow generating named path files for app routes. It is used på KTH node-web projects to pass urls to browser clients.
## PageRouter
PageRouter allows us to register page style routes that are registered in the path definition files.
Usage:
```JavaScript
const AppRouter = require('kth-node-express-routing').PageRouter
const getPaths = require('kth-node-express-routing').getPaths
const server = require('express')()
const systemRoute = AppRouter()
systemRoute.get('system.monitor', '/_monitor', function (req, res) { ... })
server.use('/', systemRoute.getRouter())
const appRoute = AppRouter()
appRoute.get('app.index', '/', function (req, res) { ... })
server.use('/', appRoute.getRouter())
const paths = getPaths()
/*
paths = {
system: {
monitor: {
uri: '/_monitor',
method: 'get
}
},
app: {
index: {
uri: '/',
method: 'get
}
}
}
*/
```
## ApiRouter
ApiRouter allows us to register api endpoints by passing api endpoint definition objects from the paths file. It will set req.scope and add the authByApiKey middleware passed to ApiRouter IF the apiDefObj passed below evaluates `apikey.scope_required == true`
Usage in your node-api app:
```JavaScript
// Middleware to protect enpoints with apiKey
const authByApiKey = passport.authenticate('apikey', { session: false })
const ApiRouter = require('kth-node-express-routing').ApiRouter
const apiRoute = ApiRouter(authByApiKey)
const apiDefObj = {
uri: "/api/node/data/:id/api/node/v1",
method: "GET",
apikey: {
scope_required: true,
scopes: ["read"],
type: "api_key"
}
}
// A middleware adding the access scope requriements (req.scope) and the authByApiKey is automatically
// prepended to the middleware pipeline
apiRoute.register(apiDefObj, function (req, res) { ... })
```