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

create-backend-project

v2.1.5

Published

```bash npx create-backend-app ``` ```bash cd <project_name> npm install ``` <br>

Downloads

235

Readme

Package: Create-backend-project

Description: Creates entire express backend project with all the necessary files and folders that follows industry standard template.

Usage:

npx create-backend-app
cd <project_name>
npm install

write your database name in the database/index.js

const DBName = ""; //replace your database name

Add your mongoDB connection string, password and CORS origin in the .env file

  MONGO_URI=mongodb+srv://USERNAME:<db_password>@cluster-test.dyzrsx0.mongodb.net/
  MONGO_PASS=abcde12345
  CORS_ORIGIN=http://localhost:3000
Folder structure:
project-name
├── src
│   ├── app.js
│   ├── server.js
│   ├── constants.js
│   ├── models
│   ├── routes
│   ├── utils
│   │   ├── catchAsyncError.js
│   │   └── appError.js
│   ├── cors
│   │   └── index.js
│   ├── database
│   │   └── index.js
│   ├── middlewares
│   │   └── error.middleware.js
│   └── controllers
│       └── error.controller.js
├── .gitignore
├── .env
└── package.json

Comoponents

  1. Error handling middleware

const handleError = (err, req, res, next) => {
  err.statusCode = err.statusCode || 500;
  err.status = err.status || `error`;
  if (process.env.NODE_ENV === `development`) {
    return sendDevelopmentError(err, res);
  }
  let error = { ...err };
  error.message = err.message;
  // console.log(error);
  // console.log(err.message);
  if (err.code === 11000) error = duplicationError(error);
  return sendProductionError(error, res);
};
  • Usage:
    This middleware will be used to handle all the errors in the application. It will send a detailed error message in the development environment and will send a user-friendly message in the production environment.
  1. CatchAsyncError

const catchAsyncError = (cb) => {
  return (req, res, next) => {
    cb(req, res, next).catch((error) => {
      next(error);
    });
  };
};
export default catchAsyncError;
  • Usage:
    1. No need to write try-catch block in the controller function.
    2. Wrap all your controller function with this catchAsyncError(controller_function) function to handle all the errors in the application.
    3. Import the class AppError from #utils/appError.js and Just throw new AppError(message, statusCode) the error will be handled automatically by the AppError class.

    Example:

    import catchAsyncError from '#utils/catchAsyncError.js';
    import AppError from '#utils/appError.js';
    
    export const getOne = catchAsyncError(async (req, res, next) => {
      const user = await User.findById(req.params.id);
        if (!user) {
            throw new AppError(`No user found with that ID`, 404);
        }
        res.status(200).json({
            status: `success`,
            data: {
                user
            }
        });
    });
  1. MongoDb Connection

import mongoose from "mongoose";

const DBName = ""; //replace your database name

const connectDB = async () => {
  const DBUrl = process.env.MONGO_URI.replace(
    `<db_password>`, //make sure names in env are correct and the password part in the uri is same like this
    process.env.MONGO_PASS
  );
  const response = await mongoose.connect(`${DBUrl}/${DBName}`);
};
export default connectDB;
  • Usage:
    1. Replace the DBName with your database name.
    2. In .env file add the mongoDB connection string into the MONGO_URI.
    3. In .env file add your mongoDb into the MONGO_PASS.

    Example:

    MONGO_URI=mongodb+srv://USERNAME:<db_password>@cluster-test.dyzrsx0.mongodb.net/
    MONGO_PASS=abcde12345
  1. CORS

import cors from "cors";

const corsOptions = {
  origin: process.env.CORS_ORIGIN || "http://localhost:8003",
  optionsSuccessStatus: 200,
  credentials: true,
};

export default cors(corsOptions);
  • Usage:
    1. Add the origin of the frontend in the .env file into CORS_ORIGIN.

    Example:

    CORS_ORIGIN=http://localhost:3000

Please raise an issue if you find any bugs or need any new features. I will try to resolve it as soon as possible.

Thank you for using this package.