NODEJS(12)Platform - strict mode - underscore-generic pool
1. strict mode
“use strict”;
If we put it in the first line of the file, that means it will take effect for global.
Some special environment
<script>
“use strict”;
console.log(“This is strict mode.”);
</script>
<script>
console.log(“This is the normal mode.”);
</script>
We can also put this “use strict” in side one function.
function strict(){
“use strict”;
return “this is strict mode”;
}
Once we use strict mode, what need us to pay attention.
v = 1; // Error, we need to define the variable before we use it.
for( i = 0; i< 2; i++){} //Error, define i before we use it.
Not using with
eval Scope
scope: global and inside function
eval for example
“use strict”;
var x = 2;
console.info(eval(“var x=5;x”)); //5
console.info(x); //2
We need put new before we create an object.
2. underscore
http://sillycat.iteye.com/blog/1862248
http://underscorejs.org/#each
3. The way to Extends One Module
Here is the way to extends the underscore lib.
“use strict”;
var _ = require(“underscore”);
_.merge = function(dest) {
….snip...
}
module.exports = _;
That means we add one more method merge to the module underscore.
4. node-pool (generic-pool)
https://github.com/coopernurse/node-pool
Test Tool
http://visionmedia.github.io/expresso/
Check the Document
http://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html#find-and-modify
Add some dependencies there>
{
"name": "buglist",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "4.2.0",
"body-parser": "1.2.0",
"generic-pool": "2.0.4",
"mongodb": "1.4.5"
}
}
Most of the changes will happen in action.js. We are using the generic pool and we are not based on monk, instead of that, we are using native mongo nodeJS driver I think.
var config = require('../config.json');
var mongodb = require('mongodb');
var poolModule = require('generic-pool');
var pool = poolModule.Pool({
name: 'mongo1',
create: function(callback){
var mongoClient = new mongodb.MongoClient(new mongodb.Server(config.host, config.port));
mongoClient.open(function(err, mongoClient) {
var db = mongoClient.db(config.dbname);
callback(err, db);
});
},
destroy: function(db) { db.close(); },
max: 10,
min: 2,
idleTimeoutMillis: 30000,
log: false
});
// Returns all the bugs
exports.getAll = function(req, res) {
pool.acquire(function(err, db){
if (err) {
res.json(500,err);
} else {
db.collection('bugs').find({}).toArray(function(err, bugs){
if (err) res.json(500, err);
else res.json(bugs);
pool.release(db);
});
}
});
};
// Creates a bug
exports.create = function(req, res) {
var body = req.body;
console.log("post body = " + body);
console.log("create title = " + body.title);
console.log("create creation = " + body.creation);
console.log("create status = " + body.status);
console.log("create assignee = " + body.assignee);
pool.acquire(function(err, db){
if (err) {
res.json(500,err);
} else {
db.collection('bugs').insert(body, function(err, bug){
if (err) res.json(500, err);
else res.json(201, bug);
pool.release(db);
});
}
});
};
// Get a bug
exports.get = function(req, res) {
var id = req.params.id;
pool.acquire(function(err, db){
if (err) {
res.json(500,err);
} else {
db.collection('bugs').findOne({_id: new mongodb.ObjectID(id) }, function(err, bug){
if (err) res.json(500, err);
else if (bug) res.json(bug);
else res.send(404);
pool.release(db);
});
}
});
};
// Updates a bug
exports.update = function(req, res) {
var id = req.params.id;
var body = req.body;
delete body._id;
pool.acquire(function(err, db){
if (err) {
res.json(500,err);
} else {
db.collection('bugs').findAndModify({_id: new mongodb.ObjectID(id)}, [['_id','asc']], {$set: body}, {multi:false, new:true}, function(err, bug){
if (err) res.json(500, err);
else if (bug) res.json(bug);
else res.send(404);
pool.release(db);
});
}
});
};
// Deletes a bug
exports.del = function(req, res) {
var id = req.params.id;
pool.acquire(function(err, db){
if (err) {
res.json(500,err);
} else {
db.collection('bugs').remove({_id: new mongodb.ObjectID(id) }, function(err){
if (err) res.json(500, err);
else res.send(204);
pool.release(db);
});
}
});
};
Debug Information
VERBOSE pool mongo1 - availableObjects.length=2 VERBOSE pool mongo1 - availableObjects.length=2 VERBOSE pool mongo1 - availableObjects.length=2
Solution:
Change the log configuration to false
var pool = poolModule.Pool({
name: 'mongo1',
create: function(callback){
var mongoClient = new mongodb.MongoClient(new mongodb.Server(config.host, config.port));
mongoClient.open(function(err, mongoClient) {
var db = mongoClient.db(config.dbname);
callback(err, db);
});
},
destroy: function(db) { db.close(); },
max: 10,
min: 2,
idleTimeoutMillis: 30000,
log: false
});
References:
http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html
http://sillycat.iteye.com/blog/1862248
http://underscorejs.org/
generic connection pool
https://github.com/zs1621/NodejsStudy/wiki/connect-mongodb
http://blog.csdn.net/zenghuaidong/article/details/7428808
http://www.cnblogs.com/enix/p/3654894.html
https://github.com/zs1621/NodejsStudy/blob/master/mongodb-connect/connect_mongodb_pool.js
- 浏览: 2560688 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 484NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 345Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 444Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 394Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 488NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 432Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 342Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 255GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 456GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 332GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 318Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 324Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 302Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 315Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 299NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 266Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 579NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 273Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 382Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 382Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
基于vue+nodejs毕业设计-在线购物商城系统(源码+数据库+文档说明)含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用,该项目可以作为毕设、期末大作业使用,...
基于vue+nodejs毕业设计-图书管理系统(源码+论文资料),该项目是个人毕设项目,答辩评审分达到98分,代码都经过调试测试,确保可以运行!欢迎下载使用,可用于小白学习、进阶。该资源主要针对计算机、通信、人工...
cos-nodejs-sdk-v5腾讯云 COS Nodejs SDK()installnpm i cos-nodejs-sdk-v5 --savedemo// 引入模块var COS = require('cos-nodejs-sdk-v5');// 创建实例var cos = new COS({ SecretId: '...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs010-nodejs-mute-stream-0.0.4-3.el6.centos.alt.noarch.rpm
nodejs010-nodejs-fstream-npm-0.1.6-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-tunnel-agent-0.3.0-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-form-data-0.1.1-3.el6.centos.alt.noarch.rpm
nodejs010-nodejs-lru-cache-2.5.0-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-fstream-ignore-0.0.7-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-http-signature-0.10.0-5.1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-node-uuid-1.4.1-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-graceful-fs-2.0.0-2.el6.centos.alt.noarch.rpm
nodejs010-nodejs-tobi-cookie-0.2.0-2.el6.centos.alt.noarch.rpm
nodejs010-nodejs-uid-number-0.0.3-7.el6.centos.alt.noarch.rpm
nodejs010-nodejs-forever-agent-0.5.0-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-oauth-sign-0.3.0-1.el6.centos.alt.noarch.rpm