- Details
- Written by C M Raja
- Category: Node JS
- Hits: 95386
Express application allows to open only one database connection pool object across the application.
In this example,
1/ mssql library is used to connect MS-SQL server
2/ one time connection pool is generated in the library file "commonDbConnection.js"
3/ above library is referred where ever its required. In our example, used it in "server.js" & "anotherlibraryfile.js"
config.js
var config = {
databaseConn: {
server: "dbserver",
database: "db",
user: "uid",
password: "pwd",
options: {
encrypt: false
}
},
Config_Key1: 'config_value1',
Config_Key2: 'config_value2',
Config_Obj: {
config_key3 : 'config_value3',
config_key4 : 'config_value4',
config_key5 : 'config_value5',
}
};
module.exports = config ;
commonDbConnection.js
It returns as a promise object "commonConnPool"
var config = require("./config").databaseConn;
const mssql = require("mssql");
const commonConnPool = new mssql.ConnectionPool(config)
.connect()
.then(pool => {
console.log("Connected to MSSQL");
return pool;
})
.catch(err => console.log("Database Connection Failed! Bad Config: ", err));
module.exports = {
mssql,
commonConnPool
};
server.js
....
var commonConnPool= require('./commonDbConnection').commonConnPool;
(or)
var {commonConnPool} = require('./commonDbConnection')
....
//While setup CORS inject the DB connection pool and later you can retrieve it in the API library
app.use(async function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
req.conn_pool = await commonConnectionPool;
next();
});
...
app.use('/api', api);
anotherlibraryfile.js
var { commonConnPool, mssql } = require('./dbInitCommonConn');
async insertIntoDB(value1, value2, value3) {
try {
await commonConnPool.then(pool => {
pool
.request()
.input("param1", mssql.VarChar(50), value1)
.input("param2", mssql.VarChar(50), value2)
.input("param3", mssql.VarChar(50), JSON.stringify(value3)) //if its object, then stringify it before pass
JSON.stringify(message)
)
.execute("usp_Insert_Record_SP");
});
} catch (err) {
console.log("error occurred", err);
}
}
- Details
- Written by C M Raja
- Category: Node JS
- Hits: 5151
Step 1 : Eject
npm run eject
This command used to moves create-react-app's configuration files and dev/build/<<appname>> scripts into you <<app>> directory.
Note:
To run this command make sure following topics
- Add any untracked files you want to keep to your Git repository.
- Remove any untracked files you don't want to keep (either by deleting them or adding them to your .gitignore)
- Commit your changes, or stash them if you'd like to commit them at a later time
- create-react-app prevents you from ejecting your app without first having committed any outstanding changes to Git
Step 2: babel's remove-console
Install babel-plugin-transform-remove-console library and make following changes in package.json configuration file
npm install babel-plugin-transform-remove-console
"babel": {
"presets": [
"react-app"
],
"plugins": [
"transform-remove-console"
]
}
Step 3: Build ReactJS
Run following command to build ReactJS application to deploy on production. It will place all the files under a folder "build"
npm run build
- Details
- Written by C M Raja
- Category: Node JS
- Hits: 5360
PM2 is straightforward, it is offered as a simple and intuitive CLI, installable via NPM. Just start your application with PM2 to boost your application and to make it ready to handle a ton of traffic!
Installation
The latest PM2 stable version is installable via NPM:
npm install pm2@latest -g
Above will install the latest version and if its running then you need update the in-memory version by running following command
pm2 update
Usage
The simplest way to start, daemonize and monitor your application is by using this command line:
pm2 start app.js
Application declaration
You can also create a configuration file to manage multiple applications:
process.yml:
apps:
- script : app.js
instances: 4
exec_mode: cluster
- script : worker.js
watch : true
env :
NODE_ENV: development
env_production:
NODE_ENV: production
And start it easily:
pm2 start process.yml
Read more about application declaration here.
Setup startup script
Restarting PM2 with the processes you manage on server boot/reboot is critical. To solve this, just run this command to generate an active startup script:
pm2 startup
Stop PM2 process
pm2 kill
Stop an instance running under PM2
pm2 stop <<instance_id>>
pm2 stop 0
If you want to stop all running instances
pm2 stop all
Cluster setup
pm2 start server.js -i max
"max" is deprecated and instead of that you can supply 0 pm2 start server.js -i 0
This will roll out maximum number of instance based on no of core available in the server
Supply parameter while initiate an instance
pm2 start server.js -- DEV
In above example, DEV is the parameter value supplied
pm2 start -i 0 server.js -- PARAM1 PARAM2
Above one roll out maximum no of instances based of no of core available and supplying PARAM1 & PARAM2
To display logs (especially the startup logs which includes console.log in express application)
pm2 logs startup
To see metrics & detail of a particular running instance
pm2 show <<instance_id>>
To monitor all running instance with metrics & meta details
pm2 monit
pm2 show 0
To see all environment variable available for the particular instance
pm2 env <<instance_id>>
pm2 env 0