I am new to MongoDB and NodeJS. I wrote a simple app that tests the write speed of a RESTFUL state API:
const express = require('express')
const port = process.env.PORT
var MongoClient = require('mongodb').MongoClient;
const router = express.Router()
router.post('/mongo-log', async(req, res) => {
try {
let syserr = false;
let e = null;
await MongoClient.connect(process.env.MONGODB_URL,async function(err,db) {
if(err)
{
console.log("-------------CONNECT ERROR----------");
console.log(err);
process.exit(1);
}
const dbo = db.db(process.env.MONGODB_NAME);
await dbo.collection("mongolog").insertOne({details:req.body.details,createDate:(new Date())},function(err,res) {
if(err)
{
console.log("-------------INSERT ERROR----------");
console.log(err);
process.exit(1);
}
});
});
if(e) throw e;
res.status(201).send({"success":true})
} catch (error) {
console.log(error);
res.status(400).send(error)
}
})
var cors = require('cors')
const app = express()
app.use(cors())
app.use(express.json())
app.use(router)
if(process.env.SSL_KEY && process.env.SSL_CRT) {
const https = require('https')
const fs = require('fs')
const options = {
key: fs.readFileSync(process.env.SSL_KEY),
cert: fs.readFileSync(process.env.SSL_CRT)
}
var server = https.createServer(options, app)
server.listen(port, () => {
console.log("server starting on port : " + port)
})
} else {
app.listen(port, () => {
console.log(`Server running on port ${port}`)
})
}
I start the app with npm start
from terminal. Then I wrote a separate python script with an infinite while loop without any sleeper/timeouts that just submits {details:{a bunch of nested objects}}
with a request.post()
.
Everything works fine for a few seconds. But the moment my API has written exactly 1084 records to mongodb, I get the following error from one of my if statements above:
-------------CONNECT ERROR----------
MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [Error: write EPIPE at afterWriteDispatched (internal/stream_base_commons.js:150:25) at writeGeneric (internal/stream_base_commons.js:141:3) at Socket._writeGeneric (net.js:776:11) at Socket._write (net.js:788:8) at doWrite (_stream_writable.js:453:12) at writeOrBuffer (_stream_writable.js:435:5) at Socket.Writable.write (_stream_writable.js:315:11) at Connection.write (/home/john/projects/dbperformance/node_modules/mongodb/lib/core/connection/connection.js:271:21) at runCommand (/home/john/projects/dbperformance/node_modules/mongodb/lib/core/connection/connect.js:384:8) at performInitialHandshake (/home/john/projects/dbperformance/node_modules/mongodb/lib/core/connection/connect.js:122:3) { name: 'MongoNetworkError', [Symbol(mongoErrorContextSymbol)]: {} }] at Pool. (/home/john/projects/dbperformance/node_modules/mongodb/lib/core/topologies/server.js:438:11) at Pool.emit (events.js:210:5) at /home/john/projects/dbperformance/node_modules/mongodb/lib/core/connection/pool.js:561:14 at /home/john/projects/dbperformance/node_modules/mongodb/lib/core/connection/pool.js:994:11 at callback (/home/john/projects/dbperformance/node_modules/mongodb/lib/core/connection/connect.js:97:5) at /home/john/projects/dbperformance/node_modules/mongodb/lib/core/connection/connect.js:124:7 at _callback (/home/john/projects/dbperformance/node_modules/mongodb/lib/core/connection/connect.js:349:5) at Connection.errorHandler (/home/john/projects/dbperformance/node_modules/mongodb/lib/core/connection/connect.js:365:5) at Object.onceWrapper (events.js:300:26) at Connection.emit (events.js:210:5) { name: 'MongoNetworkError', [Symbol(mongoErrorContextSymbol)]: {}
Why do I keep getting an error after each 1084 records?