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;
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:
const ldapCfg = {
ldapServerUrl: 'ldap://<host ip or host name>:389'
, domainNameEmail: '@<your domain name>'
, connectTimeout: `<timeout when ldap server not response>`
};
const ldapLogin = new LDAPLogin(ldapCfg.ldapServerUrl, ldapCfg.domainNameEmail, ldapCfg.connectTimeout);
let user = await ldapLogin.login(user.username, passHash);
6. Use Router following:
const { postHandler, Router } =require("cng-node-js-utils");
const { serverHandler } = require("../../handlers/server")
const functionPaths = {
POST: {
"/test": [
postHandler.jsonProcess
, serverHandler.getUserDevice
],
},
GET: {
"/test": [
postHandler.getToken
, serverHandler.verifyToken
, serverHandler.getResource
],
},
PUT: {
"/test": [
postHandler.getToken
, postHandler.jsonProcess
, serverHandler.verifyToken
, serverHandler.getResource
],
},
DELETE: {
"/test": [
postHandler.getToken
, postHandler.jsonProcess
, serverHandler.verifyToken
, serverHandler.getResource
],
},
}
module.exports = (new Router(functionPaths)).getExpressRouter();
- define index.js in ./routes as:
const apiRoutes = [
{
path: `/api`
, route: require('<your router file above as you define in 6.1> ')
}
]
module.exports = (app, ioSubDir) => {
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
const expressCfg = {
port: 8080
, baseDirectory: "/socket"
, isDebug: true
, domainIncludeCors:
[
'localhost'
]
};
const ddosUse = null;
const apiRoutes = require("./routes");
const socketIoCfg = null ;
const { ExpressServer } =require("cng-node-js-utils");
const expressServer = new ExpressServer(expressCfg, ddosUse, apiRoutes, socketIoCfg);
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