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-starter-cli

v1.0.2

Published

A CLI tool to create a basic Node.js + Express.js project with MongoDB setup

Downloads

159

Readme

Express Starter CLI

Express Starter CLI is a command-line tool that helps you quickly scaffold a Node.js + Express.js project with MongoDB. It sets up the basic structure and installs essential packages like express, mongoose, jsonwebtoken, and more. This tool saves you from repetitive setup work so you can start building your application faster.

Features

  • Creates a fully working Node.js + Express.js application.
  • Sets up a MongoDB connection using mongoose.
  • Includes JWT authentication setup with jsonwebtoken.
  • Installs necessary middleware like cors, bcryptjs, multer for handling requests and file uploads.
  • Provides a pre-configured folder structure (controllers, models, routes, middleware, utils).
  • Includes a .env.example file for environment variable configuration.
  • Adds a sample API endpoint to get you started.

Installation

To install the Express Starter CLI globally on your machine, you can use npm:

npm install -g express-starter-cli

This will make the express-starter command available globally in your terminal.

Usage

Once the CLI is installed, you can create a new project by running:

express-starter <project-name>

Replace <project-name> with the name of your project.

Example:

express-starter my-awesome-app

This command will create a folder called my-awesome-app with the following structure:

my-awesome-app/
├── config/
│   └── connect.js           # MongoDB connection setup
├── controllers/             # Controllers for your routes
├── middleware/              # Custom middleware (e.g., for auth)
├── models/                  # Mongoose models
├── routes/                  # Route handlers
├── utils/                   # Utility functions (e.g., token generation)
├── .env.example             # Example environment variable file
├── index.js                 # Main file to run the application
├── package.json             # NPM package configuration

After running the command, navigate into your project folder:

cd my-awesome-app

Running the Project

Before running the application, make sure to set up your environment variables. Copy the .env.example file to .env and update the values as needed:

cp .env.example .env

You can then start the server using Node.js:

node index.js

or using Nodemon for automatic restarts:

npm install -g nodemon
nodemon index.js

By default, the server will be running on http://localhost:5000. You can change the port by updating the PORT value in your .env file.

Folder Structure Overview

  • config/connect.js: Sets up the connection to MongoDB using mongoose.
  • controllers/: This folder is where you define functions for your routes (e.g., handling user registration, login, etc.).
  • middleware/: Any custom middleware, like JWT authentication middleware, goes here.
  • models/: Mongoose models that define the structure of your database collections.
  • routes/: Define your application’s routes and link them to the controller functions.
  • utils/: Helper functions, such as generating JSON Web Tokens, are stored here.

Sample Files:

config/connect.js

const mongoose = require("mongoose");

const connectDB = async () => {
  try {
    await mongoose.connect(process.env.MONGO_URI, {
      serverSelectionTimeoutMS: 30000,
    });
    console.log("MongoDB connected...");
  } catch (err) {
    console.error(err.message);
    process.exit(1);
  }
};

module.exports = connectDB;

index.js

const express = require("express");
const connectDB = require("./config/connect");
const dotenv = require("dotenv");

// Load environment variables from .env
dotenv.config();

// Initialize Express
const app = express();

// Connect to MongoDB
connectDB();

// Middleware to parse incoming JSON requests
app.use(express.json());

// Sample route
app.get("/", (req, res) => {
  res.send("API is running...");
});

// Get the port from environment variables or use 5000 by default
const PORT = process.env.PORT || 5000;

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

Environment Variables

The CLI generates an .env.example file where you can specify your environment variables. After copying the file to .env, update the following values:

  • MONGO_URI: MongoDB connection string (e.g., mongodb://localhost:27017/mydatabase).
  • PORT: The port where your app will run (default: 5000).
  • jwtSecret: Secret key used for signing JWT tokens.

Here’s an example .env file:

MONGO_URI=mongodb://localhost:27017/mydatabase
PORT=5000
jwtSecret=your_jwt_secret_key_here

Additional Commands

After creating your project, you may want to add additional features. You can manually install other npm packages as needed. Here are some examples:

  • For logging:
    npm install morgan
  • For validation:
    npm install express-validator

Contributing

We welcome contributions! If you'd like to add a new feature, report a bug, or improve the code, please open an issue or submit a pull request on GitHub.

To contribute:

  1. Fork the repository.
  2. Create a new branch for your feature (git checkout -b feature/your-feature-name).
  3. Commit your changes (git commit -m 'Add some feature').
  4. Push to the branch (git push origin feature/your-feature-name).
  5. Open a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

npm version downloads license