A collection of express middleware helpers.
node-ally is a very small library that has some middleware functions that I find useful.
npm install node-ally --save
node-ally is an extremely small and easy to use library, it currently contains four middleware functions, these functions are as follows;
-
errorHandler
- The error handler is used to capture errors thrown in your NodeJS application and return a message + status code based on the thrown error as the server response. -
enableCORS
- This middleware function enabled cross origin requests -
notFound
- This middleware function will return an appropriate error to the client if they hit an endpoint that doesn't exist in your API -
requestTime
- This middleware records the time of the request and puts it on the req object
import express from 'express';
import { errorHandler, enableCORS, notFound, requestTime } from 'node-ally';
const app = express();
app.use(enableCORS);
app.use(notFound);
app.use(errorHandler);
app.use(requestTime);
export default app;
supportCrossOrigin
- This is used for preflight requests from browsers, currently it returns a 200 to the preflight request while also telling the browser that it accepts cross origin requests using this will enable CORS for the route you use it on
import express from 'express';
import { supportCrossOrigin } from 'node-ally';
const router = express.Router();
router.options('/hello-world', supportCrossOrigin);
router.post('/hello-world', (req, res, next) => {
res.status(200).send('Hello World');
});
export default router;
If you want to setup node-ally for development then you're going to need to get this repo onto your local machine by forking it and then cloning it, after it's on your machine the rest of the setup is pretty simple, you just need to install the dependencies with npm install
and check everything is working with npm test
the current node version being used can be found in the .travis.yml
npm install
npm test
- 1.0.4
- ADD: Added middleware function that added the current time to the req object
- 1.0.3
- ADD: Added this readme file
- ADD: added travis CI to the project
- 1.0.2
- CHANGE: modified to only bundle index.js rather than index.js and index.test.js in final build
- 1.0.1
- FIX: removed deprecated prepublish npm script
- 1.0.0
- The initial version of the project
Grant Leadbetter – @Grantlyk – [email protected]
Distributed under the MIT license. See LICENSE
for more information.
https://github.com/grantlyk/node-ally
- Fork it (https://github.com/yourname/yourproject/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Add your changes including tests
- Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request
- Ensure the CI build passes