2.0.0 • Published 7 years ago
@justinc/express-curl v2.0.0
express-curl
Simple but effective, this ExpressJS middleware prints out the equivalent curl command. Typically this would be used as a utility for backend developers for replicating requests that may be causing problems.
Installation
Install the package by running the command below, from the root of your NodeJS project.
npm install --save express-curl
Usage
- Include the package in your ExpressJS app along with other dependencies.
 
const fs = require('fs');
const express = require('express');
// Include express-curl as a dependency
const { expressCurl } = require('express-curl');
let app = express();
...- Declare this as a middleware just above the route definitions
 
app.use(bodyParser.json());
app.use(multiplart());
...
// Include the express curl middleware
app.use(expressCurl);
...
app.use('/auth', authRouter);
app.use('/users', usersRouter);Now the stdout will print the curl.
Customise the middleware
To customise the middleware, you can require expressCurlMiddlewareFactory instead (Note: to use this module as in v1, require expressCurl):
const { expressCurlMiddlewareFactory } = require('express-curl');
app.use(expressCurlMiddlewareFactory({
    // Logs the request as a curl command using logFn.
    // Default: true
    log: true,
    // The function to use to log the curl command.
    // Default: console.log
    logFn: console.log,
    // Attaches the curl command as a string to express's `req` for access by other middleware.
    // Default: true
    attachToReq: true,
    // The express `req`'s field name to which to assign the curl command as a string.
    // Default: 'asCurlStr'
    strName: 'asCurlStr',
}));
app.use((req, res, next) => {
    console.log('I have access to asCurlStr:', req.asCurlStr);
})If you're fine with the defaults:
const { expressCurlMiddlewareFactory } = require('express-curl');
app.use(expressCurlMiddlewareFactory());A note on requiring expressCurl
This:
const { expressCurl } = require('express-curl');
app.use(expressCurl);Is the same as:
const { expressCurlMiddlewareFactory } = require('express-curl');
app.use(expressCurlMiddlewareFactory(
    attachToReq: false,
));Which is the v1 behaviour of this module.

2.0.0
7 years ago