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-bootstrapi

v1.4.9

Published

A lightweight, flexible Node.js package that simplifies the setup of Express.js applications with built-in support for database connections, CORS, logging, security middleware, and static file serving. Save time by automating common configuration tasks, a

Downloads

279

Readme

express-bootstrap

express-bootstrap is a Node.js package that simplifies the process of setting up an Express.js server by automating repetitive configuration tasks. It helps developers quickly start their projects without having to manually configure things like logging, database connections, CORS, and static file serving.

Objective

The main goal of express-bootstrap is to help developers start their projects easily and quickly by reducing the amount of repeatable configurations and installed packages. With just a few lines of configuration, your project is ready to go.

Features

  • Automatic Database Connection: Connects to MongoDB using Mongoose with just a database name.
  • Customizable Logging: Integrates with morgan for logging requests with customizable formats and emoji-based status code coloring.
  • CORS Handling: Customizable CORS options to define allowed domains, IPs, Headers and methods.
  • Helmet Integration: Adds security middleware with Helmet, configurable and easy to enable or disable.
  • Static File Serving: Easily serve multiple static folders with different paths.
  • Error Handling: Built-in error handling middleware.
  • Quick and Easy Setup: All configurations are provided in one place.

Installation

npm install express-bootstrapi

or

yarn add express-bootstrapi

Usage

Here’s a quick guide on how to use the express-bootstrap package to set up your Express.js project.

Step 1: Create a Bootstrap File

Create a new file, for example bootstrap.ts, and import the express-bootstrap package. Then, provide the necessary options to initialize the app.

import appRoutes1 from "../app/routes1";
import appRoutes2 from "../app/routes2";
const bootstrap = require("express-bootstrapi");

bootstrap({
  // use one of this approaches to initialize app routes [routes OR routesPath]
  routes: [appRoutes1,appRoutes2],
  routesPath: [
    {
      path: "/api/v2/users",
      middleware: isUser,
      routes: appRoutes1,
    },
    {
      path: "/api/v2/shop",
      middleware: null,
      routes: appRoutes2,
    }
  ],
  staticFolders: [
    {
      path: "/",
      folder: "public",
    },
    {
      path: "/static",
      folder: "static",
    },
  ],
  db: { dbName: "myDatabase" },
  cors: {
    methods: "get,post,delete,patch,put",
    customHeaders: ["customer", "authorization"],
    requiredHeaders : [
      {
        "customer-required-header": "value1",
      }
      {
        "customer-required-header2": "value2",
      }
    ],
    allowedIPs: ["127.0.0.1"],
    allowedDomains: ["example.com", "localhost"],
    allowedRoutes?: ['/app'],
    callBack?: yourCallBackFunction()
  },
  urlencoded: {  extended: true,limit: "5mb"},
  compression: { level: -1 },
  helmet: {
    active: true,
  },
  errorsHandler: (errors) => console.log(errors),
  loggerFormat: ":remote-addr 🔗 :method ➡️ :url :status :status-color ⏱️ :response-time ms",
  port: 3000,
  host: "localhost"
});

Step 2: Define Your Routes

In your routes file (e.g., app/routes.ts), define your application routes.

import express from "express";
const router = express.Router();

router.get("/", (req, res) => {
  res.send("Hello, World!");
});

export default router;

Step 3: Run the App

You can now run your app and the server will start with the configurations provided.

CORS options

| Property | Type | Description | Example | | ----------------- | ---------- | ----------------------------------------------------------------------- | ----------------------------------------------------------------------------- | | methods | String | Specifies the HTTP methods that are allowed for cross-origin requests. | "get,post,delete,patch,put" | | customHeaders | Array | Custom headers to be added to the CORS Allowed headers request. | [ { "custom-token": "tokenHere" } ] | | allowedIPs | Array | A list of specific IPs that are allowed to make requests. | [ "127.0.0.1" ] | | allowedDomains | Array | A list of domains that are allowed to make requests. | [ "example.com", "localhost" ] | | allowedRoutes | Array | A list of paths that are allowed to requests without cors restrictions. | [ "/app", "/app2" ] | | requiredHeaders | Array | A list of required headers that restrct application respond without it. | [{ "required-header1": "your-value",},{"required-header2": "your-value",},] | | callBack | Function | A Function to handle extra application features at cors level. | | | `customHandler` | `Function` | A Function to return app and db. | |

Static Folders Options

| Property | Type | Description | Example | | -------- | -------- | ------------------------------------------------------------------------ | ---------- | | path | String | The URL path where the static folder will be served. | "/" | | folder | String | The folder name in your project that contains the static files to serve. | "public" |

staticFolders: [
  {
    path: "/",
    folder: "public",
  },
  {
    path: "/static",
    folder: "static",
  },
];

Database Connection Options

| Property | Type | Description | Example | | ---------- | -------- | -------------------------------------------------------------------------------------------------- | ---------------------------------- | | uri | String | The full MongoDB connection URI string. If provided, it will override other connection properties. | "mongodb://localhost:27017/mydb" | | user | String | The username for authenticating with MongoDB. | "dbUser" | | password | String | The password for the MongoDB user. | "dbPassword" | | host | String | The MongoDB server hostname. Defaults to "127.0.0.1" if not provided. | "localhost" | | port | String | The MongoDB server port. Defaults to "27017" if not provided. | "27017" | | dbName | String | The name of the MongoDB database. | "myDatabase" | | options | Object | Additional Mongoose-specific connection options, such as autoIndex and autoCreate. | { useNewUrlParser: true } |

Example Usage:

{
  db: {
    uri: "", // or leave empty to construct from user, password, host, and dbName
    user: "dbUser",
    password: "dbPassword",
    host: "localhost",
    port: "27017",
    dbName: "myDatabase",
    options: {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    }
  }
}

With no auth and localhost configuration

{
  db: {
    dbName: "myDatabase",
  }
}

Logger Status Color Codes

| Status Code Range | Color | Description | | ----------------- | ----- | --------------------------------------- | | 401 | 🟡 | Yellow icon for Unauthorized (401) | | 5xx | 🔴 | Red icon for Server errors (5xx) | | 402 - 499 | 🟠 | Orange icon for Client errors (402-499) | | 200 - 299 | ✅ | Green icon for Success (2xx) |

urlencoded options

urlencoded options: as express.urlencoded props

compression options

compression options: as compress props

helmet options

helmet options: as helmet props

Real world exmaples

How to use package at simple express project

How to use bug tracker send by email template

How to cors handler to implement middleware approach