nodejs should be able to meet high concurrency by using asynchronous callback.
but when using postman for interface stress testing, it is found that
the next interface requests only after the previous interface gets a response, and the response time is more than one second
is it because the nodejs connection database does not use connection pooling?
is it because I"m not using redis?
or is it because there"s something wrong with my test method?
var express = require("express");
var fs = require("fs");
var path = require("path");
var crypto = require("crypto"); //
var router = express.Router();
var MongoClient = require("mongodb").MongoClient;
var ObjectID = require("mongodb").ObjectID;
var url = "mongodb://localhost:27017/books";
//
var buf = new Buffer.alloc(2048);
// app
router.post("/enroll", function(req, res){
var data = {
userName: req.body.userName,
sex: req.body.sex,
userPhone: req.body.userPhone,
userEmail: req.body.userEmail,
password: req.body.password,
status: 1
}
for(var k in data){
if(!data[k]){
res.json({code:4, content:""});
return false
}
}
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db("books");
dbo.collection("user").find({userPhone: data.userPhone}).count(function(err, num){
if(err) throw err;
if(num == 0){
//
var pwObj = encrypt(data.password);
data.password = pwObj.pw;
data.key = pwObj.key;
dbo.collection("user").insertOne(data, function(err, result) {
if (err){
res.json({code:4, content: ""});
throw err;
}
res.json({code:1, content: ""});
})
}else{
res.json({code:2, content: ""})
}
db.close();
});
});
});
// APP
router.post("/login", function(req, res){
var userPhone = req.body.userPhone;
var password = req.body.password;
console.log(userPhone);
console.log(password);
if(userPhone && password){
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db("books");
dbo.collection("user").find({userPhone: userPhone}).toArray(function(err, resArr) {
if (err) throw err;
if(resArr.length>0){
password = password + resArr[0].key;
var md5 = crypto.createHash("md5");
var r = md5.update(password).digest("hex");
if(r == resArr[0].password){
res.json({code: 1, content: resArr[0]._id});
}else{
res.json({code: 2, content: ""});
}
}else{
res.json({code: 2, content: ""});
}
db.close();
});
});
}else{
res.json({code: 4, content: ""});
}
});