MongoError: connect ECONNREFUSED 127.0.0.1:27017

My apps stopped working after upgrading from Nodejs 14 to 17. The error I got was MongoServerSelectionError: connect ECONNREFUSED ::1:27017 The solution was simply replacing localhost by 0.0.0.0. I.e. in my source code I had to change const uri = “mongodb://localhost:27017/”; const client = new MongoClient(uri); to const uri = “mongodb://0.0.0.0:27017/”; const client = new … Read more

Can’t connect to MongoDB 6.0 Server locally using Nodejs driver

Problem is, the localhost alias resolves to IPv6 address ::1 instead of 127.0.0.1 However, net.ipv6 defaults to false. The best option would be to start the MongoDB with this configuration: net: ipv6: true bindIpAll: true or net: ipv6: true bindIp: localhost Then all variants should work: C:\>mongosh “mongodb://localhost:27017” –quiet –eval “db.getMongo()” mongodb://localhost:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0 C:\>mongosh “mongodb://127.0.0.1:27017” –quiet … Read more

How can I read the data received in application/x-www-form-urlencoded format on Node server?

If you are using Express.js as Node.js web application framework, then use ExpressJS body-parser. The sample code will be like this. var bodyParser = require(‘body-parser’); app.use(bodyParser.json()); // support json encoded bodies app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies // With body-parser configured, now create our route. We can grab POST // parameters using req.body.variable_name … Read more

Node JS NPM modules installed but command not recognized

I had this same problem and fixed it by adding the ‘npm’ directory to my PATH: Right-click ‘My Computer’ and go to ‘Properties > Advanced System Settings > Environment Variables’. Double click on PATH under the ‘User variables for Username’ section, and add C:\Users\username\AppData\Roaming\npm obviously replacing ‘username’ with yours. Based on the comments below, you … Read more

What is the difference between res.send and res.write in express?

res.send res.send is only in Express.js. Performs many useful tasks for simple non-streaming responses. Ability to automatically assigns the Content-Length HTTP response header field. Ability to provides automatic HEAD & HTTP cache freshness support. Practical explanation res.send can only be called once, since it is equivalent to res.write + res.end() Example: app.get(‘/user/:id’, function (req, res) … Read more

What the scenario call fs.close is necessary

You don’t need to use fs.close after fs.readFile, fs.writeFile, or fs.appendFile as they don’t return a fd (file descriptor). Those open the file, operate on it, and then close it for you. The streams returned by fs.createReadStream and fs.createWriteStream close after the stream ends but may be closed early. If you have paused a stream, … Read more