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
- 浏览: 2539965 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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 467NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 328Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 376Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 464NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 413Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 330Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 242GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 443GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 320GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 285Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 302Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 276NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 254Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 564NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 255Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 356Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 363Supervisor 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完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs-microservice-starter, NodeJS RESTful API Microservice Starter NodeJS RESTful API Microservice v1.2.0这个存储库包含运行 NodeJS rest式 API Microservice启动器的完整配置。 要求NodeJS用于本地开
nodejs-eth-pool 它是什么? 一个以太坊的多端口层池。 先决条件 制作 必要的 特征 地层 尼斯哈什 单人采矿 多端口 可以用作代理 可变难度 如何设置和运行? sudo apt update sudo apt install make build - ...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
DigitalOcean-Nodejs-Deployment--源码.rar
nodeJs 12.22.12-x64-windows版本 nodeJs 12.22.12-x64-windows版本 nodeJs 12.22.12-x64-windows版本 nodeJs 12.22.12-x64-windows版本 nodeJs 12.22.12-x64-windows版本
nodejs-win-x64最新安装包,nodejs的官方网站下载的64位安装包,官网速度真的不敢恭维,所以发上来给大家
nodejs-file-downloader 是一个用于下载文件的简单实用程序。 它隐藏了处理流、重定向、路径和重复文件名的复杂性。 可以自动重复失败的下载。 如果您遇到任何错误或有疑问,请不要犹豫,打开一个问题。安装$ npm ...
nodejs-socketio-chat源码,是一个很不错的Android代码,有兴趣的伙伴们抽时间可以看一下把。
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs-auth-master.rar
use nodejs+express to write api,use react to ajax,use webpack to package. github:https://github.com/zhaoyu69/nodejs-express-react 有问题欢迎指出,如果有用 欢迎star★。