Code Monkey home page Code Monkey logo

express-crud-api-generator's Introduction

express-crud-api-generator 1.0.0

NPM

What is this? And how it works?

Generate crud application in express with mongoose Schema

Installation

npm i express-crud-api-generator --save

yarn add express-crud-api-generator

Parameters

It supports 2 parameters, both of which are required:

  • app - express() instance
  • options - with two required options:
    • path - specify the base route. (For example: '/posts')
    • modal - specify the mongoose schema modal.

Path generated by generator

If options.path = '/posts'

  • /posts/index : get request for all data
  • /posts/paginate/index : get request for pagination data with options page=1&limit=10 (default page=1 and limit=10)
  • /posts/create : post request for particular data
  • /posts/:id : get data with particular id
  • /posts/:id/edit' : put and update data with particular id
  • /posts/:id/delete : delete request, delete data with particular id

Usuages

Step 1: Create a new directory
Step 2: Initialize node app

npm init -y

Step 3: Add packages

 npm i express body-parser cors mongoose dotenv express-crud-api-generator --save 
 or
 yarn add express body-parser cors mongoose dotenv express-crud-api-generator

Step 4: Add .env file

MONGOURL=mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]

Step 4: Add main file index.js

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const mongoose = require('mongoose');
const DataModal = require('./modals/Data')
require('dotenv').config();

const generator = require('express-crud-api-generator')

app.use(bodyParser.json({limit: '50mb'}));
app.use(cors())

const db = process.env.MONGOURL;

//Connect to mongo
mongoose
    .connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() =>{
        console.log('Mongo Connection Successful')
        app.enable('trust proxy');
    })
    .catch(err => console.log(err));

const port = process.env.PORT || 5000;
//generate crud for the path /data
generator(app,{path:'/data',modal:DataModal})
generator(app,{path:'/antoherPath',modal:DataModal})

app.listen(port, () => console.log('Server started on port 5000'));

Step 5: Add modals like modals/Data.js

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

const DataSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
});

module.exports = mongoose.model('data', DataSchema);

Step 6: Run the project node index.js

express-crud-api-generator's People

Contributors

dipakshrestha-ads avatar lucoadam avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.