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-app-generator

v1.0.6

Published

Genrate node app with express with simple routing

Downloads

20

Readme

Express Logo

Generate NodeJs Express App

Installation -

npm install express-app-generator --save

To make node app with express framework, many things are needed i.e -

  • Environment where the server is made.
  • Generic Responses are set, in which form their is need.
  • Routing Generic need to set.
  • Logging where ther is a need.

Now what this Module Provide to us -

  • An Enviroment where server is Run on Any Perticular Port.
  • A Generic Router with unlimited middleware can be set.
  • Give Generic Response as Data ,Items etc.
  • Obvisously a Logger which helps to Log things.

The generated app Provide us some default express Features like -

  • bodyParser.json();
  • bodyParser.urlencoded({extended: true});
  • bodyParser({ limit: '50mb', keepExtensions: true })
  • 'view engine', 'ejs'
  • express static path public folder in root of app folder
  • if port is null it use default port 3000

Generate Express App

let appGenrator=require('express-app-generator');
appGenrator.generate(3789, 'src/api', function(err,app){
   if(err){
      return console.log(err);
   }
   // your express app is Ready to use
   //you can check this on http://localhost:3789
   console.log(app)
});

If you want to change express properties , you can change it by update app.use() or app.set();

Routing

  • Clean and Easy To Use.

  • Required Things

    Provide the API path (where all the API's are kept) from the root of APP. This will be string like 'src/api' and in this apifolder all the .js files will be like users.jsnotuser.js,employees.jsnotemployee.js which meanssores` sufix is must.

    The logs folder is required ,this folder is use for logging the logs in json files ,which we get as in console

  • This Router Provides Simple CRUD Mechanism . In which Combinations of Arrays and Objects are use To Build it.

  • appRouter method which is present in app generator helps to make Routing in Flexible way.

Lets get started for Routing - REST or CRUD

    let api= app.appRouter; 
    //app is Express genrated app (above define how to get it)
    api.model('users')
        .register('REST', [ authentication ]);

This will make URL as http://localhost:3789/api/users .

We can use REST or CRUD Keyword to get create,update,delete,get,search these methods ready to use but for this, You must have all these methods present in /api/users.js file .

The array which is present just after the REST is use for middlewares. Suppose I want to make authentication every time so the function of authentication will be pass in the middleware Array.

Things REST Or CRUD Gives -

  • If the Request is for POST then it go for expots.create=(req,res)=>{};

  • If the Request is for GET then it go for expots.get=(req,res)=>{}; http://localhost:3789/api/users/:id

  • If the Request is for GET then it go for expots.search=(req,res)=>{};

http://localhost:3789/api/users Here many query params ? can use.

  • If the Request is for PUT then it go for expots.update=(req,res)=>{};

  • If the Request is for DELETE then it go for expots.delete=(req,res)=>{};

Note - All the the Middleware fuctions must have parameters req,res,next where req,res are request response express generated objects and next is the callback function which will call when one middleware work is DONE and it will go to next middleware function (if present) in that array and perform same, then go the main function in the users.js.

User Define Requests -

  • In register action and method is Requied
  • fiter for One Middleware and filters for Multiple
  • For Single Request -
    api.model('users')
        .register({
            action: 'GET',
            method: 'get', // method must present inside users.js
            url: '/:id',
            filters: [authentication] //middlewares 
        });
        
        // URL will be -
        // http://localhost:3789/api/users/:id
  • For Multiple Requests -
 api.model('users')
        .register([{
            action: 'POST',
            method: 'create'
        },{
            action: 'POST',
            method: 'createWithProfile',
            url: '/withProfile',
            filter: authentication
        }, {
            action: 'POST',
            method: 'importer',
            url: '/importer',
            filter: authentication
        }, {
            action: 'POST',
            method: 'createWhenSignIn',
            filter: authentication
        }, {
            action: 'PUT',
            method: 'update',
            url: '/:id',
            filters: [authentication, groupCode]
        }, {
            action: 'GET',
            method: 'get',
            url: '/:id',
            filters: [authentication, groupCode]
        }, {
            action: 'GET',
            method: 'search',
            filters: [authentication, groupCode]
        }, {
            action: 'GET',
            method: 'searchInGroup',
            url: '/from/group',
            filters: [authentication, groupCode]
        }, {
            action: 'GET',
            method: 'getLeader',
            url: '/get/leader/:id',
            filters: [authentication, groupCode]
        }]);

Note: api keyword in URL is Constant all over the Router. So your base url will be http://localhost:3789/api/ .

Responses

Their are Four Types of Responses All the Responses is of status Code is 200 .

  • data

    • It can take one or two params data,message, data is mandatory.
    • It is use as -
    res.data({
        name:"Leo",
        age:21
    });
      
     //res which you get from any query
    • This will give Response -
    {
        "isSuccess":true,
        "data":{
            "name":"Leo",
            "age":21
            }
    }
  • page

    • It can take one or two params items, pageSize, pageNo, totalRecordsCount items is mandatory.
    • Can also use for Paginations.
    • It is use as -
    res.page([{
        name:"Leo",
        age:21
    },{
        name:"Hardy",
        age:22
    }]);
      
     //res which you get from any query
    • This will give Response -
    {
        "isSuccess":true,
        "items":[{
             "name":"Leo",
             "age":21
         },{
            "name":"Hardy",
            "age":22
         }],
         pageSize:2
    }
  • success

    • It can takes one params message, it is mandatory.
    • Can Use for Sending Successful Signature message.
    • It is use as -
    res.success(`user successfully deleted`);
    • This will give Response -
    {
        "isSuccess":true,
        "message":"user successfully deleted"
    }
  • failure

    • It can takes two params error, message, error it is mandatory.
    • Can Use for Sending Error Signature message.
    • It is use as -
    res.failure(`user Hardy already present`);
    • This will give Response -
    {
        "isSuccess":false,
        "message":"user Hardy already present"
    }