micro-ex-router
Express style router for zeit's micro.
Table of Contents
- Install
- Features
- Use
- Use of subrouters
- Use of Compression, CORS and other NodeJs Middlewares
- Use of Express middlewares
- Serve files from a directory and render a string as html
- Available methods
- Parsed body and query by default
- Tests
- Contributing
- Legal
Install
This is a Node.js module available through the npm registry:
// With npm$ npm install micro-ex-router// With yarn$ yarn add micro-ex-router
Features
- Default returned responses.
- Parametrized routes.
- Parse body by default
- Parse query by default
- "Use" middlewares.
- "Use" middlewares for each method.
- Arrays of middlewares.
- Mix single middlewares and array of middlewares.
- Use of subrouters.
- Serve files directory
- Render string as html
Use
let micro = ;let Router = ;let defaultOptions = parseBody: true // Tells the router to parse the body by default limit: '1mb' // How much data is aggregated before parsing at max. It can be a Number of bytes or a string like '1mb'. encoding: 'utf8' acceptedMethods: 'get' 'post' 'put' 'patch' 'delete' 'head' 'options' 'use' // The methods that will be handled by the router; // Create a new routerlet router = ; router // Use middlewares available for all requests // Use middlewares available for all requests restricted by each method // Only for GET requests // Only for POST requests // Only for PUT requests // Only for PATCH requests // Only for DELETE requests // Only for HEAD requests options console // Only for OPTIONS requests // Default returned responses // Parametrized paths // Sequenced middlewares no need to use next callback, // All middlewares are processed as async // The first returned response will be sent to the client // Default parsed body for all methods other than get // Based on the Content-Type parses json, formurlencoded, text and and buffer // Default parsed query parameters // if you call /hello/with/query/params?hello=world // then req.query = {hello: "world"} // Mix single and array of middlewares ; // Init micro serverlet server = ; server;
Use of subrouters
let micro = ;let Router = ; // Create a sub routerlet subrouter = ;subrouter ; // Create a new routerlet router = ;router ; // Init micro serverlet server = ; server;
Use of Compression, CORS and other NodeJs Middlewares
let micro = ;let Router = ;let compression = ;let cors = ; // Create a new routerlet router = ; router // Add cors middleware // its the same as // .use((req, res) => new Promise(resolve => cors()(req, res, resolve))) // Add compression middleware ; // Init micro serverlet server = ; server;
Use of Express middlewares
let micro = ;let Router = ; /** * Load and configure the express winston middleware */let transports = transports;let expressWinston = ;let loggerOptions = transports: colorize: true meta: false msg: '{{res.statusCode}} {{req.method}} {{req.url}} {{res.responseTime}}ms' colorStatus: true; // Create a new routerlet router = ; router // Add expressWinston middleware ; // Init micro serverlet server = ; server;
- Note that not all express plugins will work with this router. Express modify the request object adding more properties that other plugins may use. So, if the plugin use this properties then it will not work with
micro-ex-router
.
Serve files from a directory and render a string as html
let micro = ;let Router = ; // Create a new routerlet router = ; // To render with this headerslet htmlWithHeaders = 'Cache-Control': 'public, max-age=2592000' Expires: Date + 604800000; let defaultHeaders = any: 'Cache-Control': 'no-cache' css: 'Cache-Control': 'public, max-age=2592000' Expires: Date + 604800000 ; let refreshFileListTime = 1000 * 60 * 60; // 60 minutes router // Serve files from a directory // Serve a single file // Serve a string as html // Serve a string as html returned by a function // Serve a string as html with custom headers ; // Init micro serverlet server = ; server;
Available methods
- Get
- Post
- Put
- Patch
- Delete
- Head
- Options
Parsed body and query by default
Based on the Content-Type header, the router will try to parse the body.
- If type === 'application/json' then parse the body as json
- If type === 'application/x-www-form-urlencoded' then parse the body as form urlencoded
- If type === 'text/html' then parse the body as text
- If other type than the previous ones parse the body as buffer
If it receive query params, these params will be parsed and will be available on the req.query
property
Tests
npm test
Contributing
- Use prettify and eslint to lint your code.
- Add tests for any new or changed functionality.
- Update the readme with an example if you add or change any functionality.
Legal
Author: Masquerade Circus. License Apache-2.0