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-mongo-express

v1.1.8

Published

A command-line interface (CLI) tool to spin up an Express.js application with MongoDB.

Downloads

3

Readme

Create Mongo Express

npm version License

Create Mongo Express is a simple and powerful CLI tool that automates the setup of an Express.js application with MongoDB connection. With this tool, you can quickly generate a fully configured Express.js project, complete with file based preconfigured routes and seamless integration with MongoDB.

Installation

To install the package, make sure you have Node.js and npm installed on your machine. Then, run the following command:

npx create-mongo-express project-name

Next, you will be asked to provide the MongoDB URI.

Enter the MongoDB URI:

Ensure that your MongoDB URI is in the following format:

mongodb+srv://mongoDbUser:[email protected]/databaseName?retryWrites=true&w=majority

Running the Server

After the setup is complete, navigate to the project folder:

cd project-name

Next, start the server by running the following command:

npm run dev

Your server will run on port 3500 by default.

If you wish to change the port, you can modify the server.js file located in the root folder.

Creating a New Route

To create a new route in the api folder, follow these steps:

Inside the project folder, locate the api folder. This is where you can add your custom routes.

Create a new file with a descriptive name for your route. For example, if you want to create a route for managing products, you can create a file named products.js inside the api folder.

Open the newly created file and add the necessary code to define your route. Here's an example:

const express = require('express');
const router = express.Router();
const Customer = require('../model/Customer');

// GET /api/customers
router.get('/', (req, res) => {
  res.send('This is the /customers route');
});

// GET /api/customers/:id
router.get('/:id', (req, res) => {
  console.log(req.params.id);
  res.send(`This is the /customers/${req.params.id} route`);
});

// POST /api/customers
router.post('/', async (req, res) => {
  const { first_name, last_name, email, phone } = req.body;

  if (!email || !first_name || !last_name || !phone) {
    return res
      .status(400)
      .json({ message: 'Please fill out all the required fields' });
  }

  try {
    const duplicateCustomer = await Customer.findOne({ email }).exec();

    if (duplicateCustomer) {
      return res.status(409).json({
        message: `Another user with email ${email} already exists, login instead`,
      });
    }

    // Create and store new user
    const result = await Customer.create({
      first_name,
      last_name,
      email,
      phone,
    });

    return res.status(200).json({
      message: 'User created successfully',
      first_name: result.first_name,
      last_name: result.last_name,
      email: result.email,
      phone: result.phone,
    });
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
});

module.exports = router;

Route exports

Make sure all your routes are exported properly like below, otherwise your it would show a 404 page.

const express = require('express');
const router = express.Router();

// GET /api/products
router.get('/', (req, res) => {
  res.send('This is the /products route');
});

module.exports = router;

Customize the route logic according to your requirements. You can retrieve data from the database, create new records, update existing records, or delete records based on the HTTP methods and route paths you define.

Save the file.

Your new route is now available in the Express server. You can access it using the route path you defined. For example, if you created a products.js route as shown above, you can access the route endpoints at /api/products, /api/products/:id, etc.

That's it! You have successfully created a new route in the api folder. Feel free to add more routes and customize them as per your application's needs.

Mongoose models

You can add your own mongoose models in the model folder at the root level. Here's an example:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const customerSchema = new Schema({
  first_name: String,
  last_name: String,
  email: String,
  phone: String,
});

module.exports = mongoose.model('Customer', customerSchema);