Constructor
new Router(request, response)
Parameters:
Name | Type | Description |
---|---|---|
request | CloudFrontRequest | CloudFront request object. |
response | CloudFrontResponse | | CloudFront response object (optional). |
Example
exports.handler = (event, context, callback) => {
const {request, response} = event.Records[0].cf;
const router = new Router(request, response);
callback(null, router.response());
};
..
exports.handler = async (event) => {
const {request, response} = event.Records[0].cf;
const router = new Router(request, response);
return await router.response();
};
Methods
default(route)
Set router fallback (default route).
Parameters:
Name | Type | Description |
---|---|---|
route | function | Route function. |
Example
router.default(function(req, res) {
res.status(404).send();
});
delete(path, route)
Handle HTTP DELETE requests.
Parameters:
Name | Type | Description |
---|---|---|
path | String | Request URI. |
route | function | Route function. |
Example
router.delete('/api/test', function(req, res) {
res.status(200).send();
});
get(path, route)
Handle HTTP GET requests.
Parameters:
Name | Type | Description |
---|---|---|
path | String | Request URI. |
route | function | Route function. |
Example
router.get('/api/test', function(req, res) {
res.setHeader('Content-Type', 'text/html');
res.status(200).send('Hello World');
});
handle(path, func)
Handle the Route/Middleware request (add to stack).
Parameters:
Name | Type | Description |
---|---|---|
path | String | Request URI. |
func | function | Route/Middleware function. |
patch(path, route)
Handle HTTP PATCH requests.
Parameters:
Name | Type | Description |
---|---|---|
path | String | Request URI. |
route | function | Route function. |
Example
router.patch('/api/test', function(req, res) {
res.status(204).send();
});
post(path, route)
Handle HTTP POST requests.
Parameters:
Name | Type | Description |
---|---|---|
path | String | Request URI. |
route | function | Route function. |
Example
router.post('/api/test', function(req, res) {
res.status(201).send();
});
put(path, route)
Handle HTTP PUT requests.
Parameters:
Name | Type | Description |
---|---|---|
path | String | Request URI. |
route | function | Route function. |
Example
router.put('/api/test', function(req, res) {
res.status(201).send();
});
response() → {CloudFrontResponse|Promise.<CloudFrontResponse>}
Return CloudFront response object.
Returns:
- Type:
- CloudFrontResponse |
Promise.<CloudFrontResponse>
setPrefix(value)
Set URI path prefix.
Parameters:
Name | Type | Description |
---|---|---|
value | String | Request URI. |
Example
router.setPrefix('/api');
use(arg, func)
Load the Route (e.g. Middleware) handler.
Parameters:
Name | Type | Description |
---|---|---|
arg | function | | Route/Middleware or Request URI. |
func | function | Route/Middleware function (optional). |
Example
// Include function as middleware.
const Middleware = require('./path/to/middleware');
router.use(Middleware);
..
// Run function for every request.
router.use(function(req, res, next) {
if (req.method() === 'POST') {
res.status(405).send();
} else {
next();
}
});
..
// Run function on URI path only.
router.use('/api/test', function(req, res) {
res.setHeader('Content-Type', 'text/html');
res.status(200).send('Hello World');
});