Mongoose autoReconnect option

I had the same question as you, and robertklep’s solution didn’t work for me either. I found when MongoDB service is stopped, an error event is triggered, but the connection.readyState is still 1 (connected). That may be why it didn’t auto reconnect. This is what I have now: var db = mongoose.connection; db.on(‘connecting’, function() { … Read more

Securing my API to only work with my front-end

Apply CORS – server specifies domains allowed to request your API. How does it work? Client sends special “preflight” request (of OPTIONS method) to server, asking whether domain request comes from is among allowed domains. It also asks whether request method is OKAY (you can allow GET, but deny POST, …) . Server determines whether … Read more

nodejs v8.getHeapStatistics method

Some good explanation from gc-heap-stats package: total_heap_size: Number of bytes V8 has allocated for the heap. This can grow if usedHeap needs more. used_heap_size: Number of bytes in used by application data total_heap_size_executable: Number of bytes for compiled bytecode and JITed code heap_size_limit: The absolute limit the heap cannot exceed (default limit or –max_old_space_size) total_physical_size: … Read more

What is the difference between “express.Router” and routing using “app.get”?

Here’s a simple example: // myroutes.js var router = require(‘express’).Router(); router.get(“https://stackoverflow.com/”, function(req, res) { res.send(‘Hello from the custom router!’); }); module.exports = router; // main.js var app = require(‘express’)(); app.use(‘/routepath’, require(‘./myroutes’)); app.get(“https://stackoverflow.com/”, function(req, res) { res.send(‘Hello from the root path!’); }); Here, app.use() is mounting the Router instance at /routepath, so that any routes added … Read more