npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-api-to-postman

v1.0.7

Published

Automated Postman collection creation from Express Node APIs.

Downloads

194

Readme

express-api-to-postman

express-api-to-postman is a Node.js package that automates the generation of a Postman collection from your Express API routes. It helps developers quickly document and test their APIs by extracting route information from an Express app and converting it into a Postman collection file.

Features

  • Automatically extracts all routes from an Express app
  • Supports multiple HTTP methods (GET, POST, PUT, DELETE, etc.).
  • Cleans and structures routes into a Postman-compatible collection
  • Easy to use allowing users to specify the server file and the desired collection name.
  • Generates a Postman collection in JSON format.

Installation

Install express-api-to-postman with npm

npm i express-api-to-postman

Usage

After installing the package, you can run it using the following command:

express-api-to-postman <serverFilePath> <collectionName>

Once the command is executed, a Postman collection file will be created, which can be imported into Postman for testing.

Things to Keep in Mind

1. Server File Requirements

  • The Express app should export the Express instance, typically assigned to app. This allows express-api-to-postman to access the app and extract the route definitions.
  • Ensure that the file path provided to express-api-to-postman is correct and points to the file where your Express app is instantiated.

2. Handling of Nested Routes

  • If your Express app contains nested routes (e.g., via express.Router()), these routes will also be detected and added to the Postman collection. The tool handles this by recursively traversing the app's router stack and normalizing route paths.

3. Custom Methods and Middleware

  • Routes that use custom middleware or HTTP methods (like PATCH or OPTIONS) will be included as long as they are explicitly defined in your Express app.
  • The collection generator currently selects the first defined HTTP method when multiple methods are found for the same route. You may want to review and adjust the collection if you have routes that respond to multiple methods.

4. Postman Collection URL

  • The generated collection uses a placeholder {{base_url}} for the API’s base URL, which you can replace with your actual base URL in Postman.
  • The collection file does not automatically infer the API’s host or port number, so remember to set the base_url environment variable in Postman before making requests.

5. Error Handling

  • If the tool is unable to load your server file or finds no routes in the Express app, an error will be logged. Ensure that your Express app is correctly exporting the app object.
  • If no routes are found, double-check the provided file path, and confirm that routes are defined before running the command.

6. Generated Postman Collection

  • The generated Postman collection will have an individual item for each route in your Express app. Each item will be structured with the following:
    • The route’s method (GET, POST, etc.).
    • A placeholder for request bodies (for methods like POST and PUT).
    • A default {{base_url}} variable in the URL field.

You may modify these defaults as needed once the collection is imported into Postman.

Sample Code

Here's a sample Express app (server.js) to demonstrate usage with express-api-to-postman:

// server.js
import express from 'express';

const app = express();

app.get('/users', (req, res) => {
  res.json({ message: 'List of users' });
});

app.post('/users', (req, res) => {
  res.json({ message: 'User created' });
});

app.put('/users/:id', (req, res) => {
  res.json({ message: `User ${req.params.id} updated` });
});

app.delete('/users/:id', (req, res) => {
  res.json({ message: `User ${req.params.id} deleted` });
});

export default app;

To generate a Postman collection from this app:

express-api-to-postman ./server.js MyApiCollection

Collaborations are Welcome!

This package is open to collaboration, and I am eager to work with other developers to enhance and expand its capabilities. Whether you want to suggest improvements, report issues, or contribute code, your involvement is highly appreciated.

Here are some ways to contribute:

  • Submit a pull request to add new features or fix bugs.
  • Open an issue to report bugs, suggest new features, or share feedback.
  • Share this package with others who might find it useful.
  • Join the discussion by providing input on open issues or ideas for future updates.

If you’re interested in collaborating or have any questions or feedback, feel free to reach out to me via LinkedIn or email me at [email protected].

Linkedin

Authors