when node.js connects to mongodb and executes MongoClient.connect (), it is very fast in the local environment, but very slow to get online, which takes more than 1 second, as shown below:
var express = require("express");
var router = express.Router();
var MongoClient = require("mongodb").MongoClient;
var url = "mongodb://localhost:27017/";
var tools = require("../../public/tools/tools")
/* GET home page. */
router.get("/", function(req, res) {
//
console.time("find")
MongoClient.connect(url, {useNewUrlParser:true},function(err, db) {
console.timeEnd("find") //
if (err) throw err;
var dbo = db.db("qianyanjiu");
var mysort = { postdate: -1 };
where = {status: 1}
dbo.collection("news").find(where).sort(mysort).limit(5).toArray(function(err, result) {
if (err) throw err;
for(var i = 0; i < result.length; iPP){
result[i].postdate = tools.getDate(result[i].postdate)
}
db.close();
res.render("web/index", {
news:result
});
});
});
});
the speeds are as follows:
one needs more than 1s, one only needs 10ms, and the other needs to load more than 1s online to display the web page.
ps: the server is an Ali Cloud single-core 2g efficient cloud disk, and the local i7 16g ssd is related to the configuration.