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

cng-node-js-utils

v3.0.4

Published

for nodejs utils in server

Downloads

219

Readme

EASY-EXPRESS-API-SERVER-FOR-PRIVATE-CLOUD - lib utils for nodejs include post, get, ip, ldap, token, waiting, validators

1. install

npm i cng-node-js-utils@latest

2. index.js of this libs

const {
  expHandlers,
  validators,
  arrObj,
  dateUtil,
  excelUtils,
  expUtil,
  htmlUtil,
  ipInfo,
  jsEncrypt,
  ldapLogin,
  myDevice,
  pdfUtil,
  postHandler,
  qrCodeUtil,
  rsaService,
  Router,
  secretUtil,
  sendGmail,
  uniqueid,
  vnHandler,
  waiting,
  webService,
  createSubFolder,
  convertPath2ObjectName,
  createEntryIndex,
  captchaUtil
} = require("cng-node-js-utils")

console.log("This time:", arrObj.getTimestamp());

3. Set req.functionCode for validate Right of Api

// 
const { setFunctionFromPath } = require("cng-node-js-utils").expHandlers;
// if (req.isFullPath) then req.functionCode = req.pathName; 
// return req.functionCode = subpath;

4. Send Gmail by API very easy:

  const { sendGmail } =require("cng-node-js-utils");
  sendGmail((receiver, subject, content, senderGmail, passGmail, fromEmail, host = 'smtp.gmail.com', port = 465, secure = true)
  .then(ok=>console.log(ok))
  .catch(err=>console.log(err))

5. Use LDAP following:

// define your config to your ldap server
const ldapCfg = {
    ldapServerUrl: 'ldap://<host ip or host name>:389'
    , domainNameEmail: '@<your domain name>'
    , connectTimeout: `<timeout when ldap server not response>`
};
// init your connection
const ldapLogin = new LDAPLogin(ldapCfg.ldapServerUrl, ldapCfg.domainNameEmail, ldapCfg.connectTimeout);
// for verify LDAP
let user = await ldapLogin.login(user.username, passHash);
// if error no user return;

6. Use Router following:

  • 6.1 define one router:
// in this library,  FROM 0.0.5  for auto web API with req.error and req.finalJson in Router
const { postHandler, Router } =require("cng-node-js-utils");

// user custom class or functions:
const { serverHandler } = require("../../handlers/server")

const functionPaths = {
   // for POST method = CREATE = insert/import
    POST: {
        "/test": [
            postHandler.jsonProcess        
            , serverHandler.getUserDevice   
            // ... in the Router, auto include 2 function:
            // , checkErrorBeforeResource     // return res(435, {error}) for req.error
            // , sendFinnalJson               // return res(200, req.finalJson or {})
            // ... so you do not add more function for response if you want
        ],
        //...
    },
    // for GET method = select
    GET: {
        "/test": [
            postHandler.getToken              // return reg.token
            , serverHandler.verifyToken       // return req.user or req.error
            , serverHandler.getResource       // web response => return req.finalJson for user api and next()
            // ... in the Router, auto include 2 function:
            // , checkErrorBeforeResource     // return res(435, {error}) for req.error
            // , sendFinnalJson               // return res(200, req.finalJson or {})
            // ... so you do not add more function for response if you want
        ],
        //...
    },
    // UPDATE = update
    PUT: {
        "/test": [
            postHandler.getToken              // return reg.token
            , postHandler.jsonProcess    
            , serverHandler.verifyToken       // return req.user or req.error
            , serverHandler.getResource       // web response => return req.finalJson for user api and next()
            // ... in the Router, auto include 2 function:
            // , checkErrorBeforeResource     // return res(435, {error}) for req.error
            // , sendFinnalJson               // return res(200, req.finalJson or {})
            // ... so you do not add more function for response if you want
        ],
        //...
    },
    // DELETE = delete
    DELETE: {
        "/test": [
            postHandler.getToken              // return reg.token
            , postHandler.jsonProcess
            , serverHandler.verifyToken       // return req.user or req.error
            , serverHandler.getResource       // web response => return req.finalJson for user api and next()
            // ... in the Router, auto include 2 function:
            // , checkErrorBeforeResource     // return res(435, {error}) for req.error
            // , sendFinnalJson               // return res(200, req.finalJson or {})
            // ... so you do not add more function for response if you want
        ],
        //...
    },
}
// export router of Express the same old version
module.exports = (new Router(functionPaths)).getExpressRouter();
  • define index.js in ./routes as:
// define api path for many router .... as following
const apiRoutes = [
    // is one router as https://yourhost/yourBaseDir/api --> function in router
    {
        path: `/api`
        , route: require('<your router file above as you define in 6.1> ')
    }
    // ,
    // ... another router
]
module.exports = (app, ioSubDir) => {
    // Init routers for aps in express server
    for (let r of apiRoutes) {
        console.log("*> THE ROUTES of this server:", `${ioSubDir}${r.path}`);
        app.use(`${ioSubDir}${r.path}`, r.route);
    }
}

7. Express server for Easy config only one minute. SAMPLE SERVER: make server following in file ./test-server-express.js

// define config
const expressCfg = {
    // Port for server
    port: 8080
    // base
    , baseDirectory: "/socket"
    // If you want https define:
    // , https: {
    //     privateKey: "./cert/private.key",
    //     certificate: "./cert/certificate.crt"
    // }
    // default404Page:`<h1>Error Page 404 if you want</h1>`,
    // static html for root
    // , staticRoot: __dirname + '/client-c3-www/www'
    // static for baseDirectory
    // , staticHtml: __dirname + '/client-c3-www/socket-www/www'
    // if debug
    , isDebug: true
    // include cors for API web
    , domainIncludeCors:
        [
             'localhost'
        ]
};

// if you want to block ddos you have to install dddos:
const ddosUse = null; //= require('./ddos/ddos-config').express('ip', 'path'),

// define route follow # 6
const apiRoutes = require("./routes");

// if you want to run socketIo, you have to define socketIo 
const socketIoCfg = null ; //require('./server-socketio');

// include main libarary
const { ExpressServer } =require("cng-node-js-utils");
// define class
const expressServer = new ExpressServer(expressCfg, ddosUse, apiRoutes, socketIoCfg);
// start server
      expressServer.start();

8. run and test server SAMPLE very easy:

node ./test-server-express.js

Test server in: http://localhost:8080 and see web, and some API you define in routers. EASY