I have tested the code with a localhost and it works fine however when I try to test the connection on the server I get the error
/node_modules/mongodb/lib/operations/add_user.js:16
this.options = options ?? {};
The process is npm init and give the project a name npm install mongodb then run node file, it seems like the error is on the package as when I run node without the MongoClient the node runs and running this script (locally on a window machine the server is linux) it works even with the same ip address.
Here is the code I use for the connection
const { MongoClient } = require("mongodb").MongoClient;
const http = require('http');
const hostname = 'preworn.co.uk';
const port = 3000;
const uri ="mongodb://"SERVER IP ADDRESS":27017/";
const client = new MongoClient(uri);
const server = http.createServer((req, res) =>
{
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
async function run()
{
try
{
await client.connect();
const db = client.db("preworn_market");
const specifics = db.collection("product_specifics");
const cursor = specifics.find();
await cursor.forEach(function(myDoc) { res.write("<h4>"+myDoc.category+"</h4>"); });
}
finally
{
await client.close();
res.end("");
}
}
run().catch(console.dir);
});
server.listen(port, hostname, () =>
{
console.log(`Server running at http://${hostname}:${port}/`);
});