i want to connect node js app with mongodb in windows pc but am having a problem connecting - mongodb

this is the problem on the image
(https://i.stack.imgur.com/amXj4.png)(https://i.stack.imgur.com/amXj4.png)
const express = require("express");
const app = express();
const MongoClient = require("mongodb").MongoClient;
const assert = require("assert");
const url = "mongodb://localhost:27017";
const dbname = "myff";
const client = new MongoClient(url);
client.connect(function(err){
assert.equal(null, err);
console.log("success");
const db = client.db(dbname);
client.close();
});
app.listen(3000, function(){
console.log("3000");
});

Related

Mongo db connection is not establishing

// mongo.js
const express = require('express');
const router = require('./router');
const mongoConnection = require('./mongo')
const cors = require('cors');
const bodyParser = require('body-parser')
const api = express();
const port = 3000;
api.listen(port, ()=>{
console.log('port is running successfully');
});
api.use(cors())
api.use(bodyParser.urlencoded({ extended: true }));
api.use(bodyParser.json())
api.use(express.json());
api.use('/api', router);
mongoConnection();
// index.js:
require('dotenv').config();
const {MongoClient} = require('mongodb');
const mongoUrl = process.env.DB_URL;`
const mongoConnection = async () => {
try {
const dbConnection = await mongoose.connect(mongoUrl);
console.log("DB is connected", dbConnection);
} catch {
} finally {
await mongoose.disconnect();
}
}
module.exports = mongoConnection;
Hi Team, I've hard time connecting my DB is here. Earlier It was doing fine and showing "port running successfully" and "Db is connected". Now Db connection is not establishing and can't see the error also.
I tried, Sudo mongod also and not working. Any leads would help, thanks in advance.

phantomjs unable to open local file

I am able to get the content of http://example.com but not the index.html which is in my code with phantomjs. I see the filePath is correct.
'use strict';
const express = require('express');
const phantom = require('phantom');
const path = require('path');
// Constants
const PORT = 8888;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
(async function() {
const instance = await phantom.create();
const page = await instance.createPage();
await page.on('onResourceRequested', function(requestData) {
console.info('Requesting', requestData.url);
});
const status = await page.open(getFileUrl('index.html'));
const content = await page.property('content');
console.log(content);
await instance.exit();
})();
});
function getFileUrl(str) {
var pathName = path.resolve( str );
if (pathName[0] !== "/") {
pathName = "/" + pathName;
}
return encodeURI("file://" + pathName);
};
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
Output
Running on http://0.0.0.0:8888
Requesting file:///home/codex/code/target-data-app/index.html
<html><head></head><body></body></html>

How to connect to Mongo db server remotely through its default ports?

I'm trying to connect to a mongo db server remotely through the default port to view the db contents. Suggest me how to do that remotely , it could also be either through python or node.js.
To connect remotely to a mongodb server, then all you will need is :
*. database URI
Which comprises of database name and authentication if needed
Sample code for connecting to remote mongodb shared atlas cluster is like this:
const express = require('express')
const app = express()
var cors = require('cors')
const { MongoClient } = require('mongodb')
const uri = 'mongodb+srv://xxx-sampledb:xxx-mongodb-
sampledb#sandbox.xxxx.mongodb.net/ellasShop?authSource=admin';
//middleware
app.use(express.json())
app.use(cors())
//initialize db connectivity options
const options = {
useUnifiedTopology: true,
useNewUrlParser: true,
}
app.get('/api/products', async (req, res) => {
const client = new MongoClient(uri, options);
try {
await client.connect();
const database = client.db('ellasShop')
const collection = database.collection('productData')
const products = await collection.findOne();
return res.json(products);
} catch (err) {
console.log(err)
} finally{
await client.close()
}
})
app.listen(5000, () => {
console.log('Server is running')
})

Unable to connect to Mongodb using moongoos

I am trying to create DB using following snippet
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const ejs = require("ejs");
const app = express();
const port = 80;
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
mongoose.connect("mongodb://localhost:27017/griData", {useNewUrlParser: true, useUnifiedTopology: true}, (err)=> {
if(err){
console.log(err);
}
});
console.log(mongoose.connection.readyState);
app.get("/", (req, res)=> {
res.render("main");
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
but when I check dbs from mongo shell, I don't see any db. What am I missing? Thanks

Unable to fetch data from mongodb using expressjs

const express = require('express');
const bodyParser = require('body-parser');
const mongodb = require('mongodb');
const app = express();
const cors = require('cors');
app.use(bodyParser.json());
app.use(cors());
const MongoClient = mongodb.MongoClient;
const url = "mongodb://localhost:27017/recart";
app.get("/", (req, res) => {
MongoClient.connect(url,{ useNewUrlParser: true }, async (err, db) => {
if (err) throw err;
var dbo = db.db("recart");
var result = await dbo.collection("users").find()
res.json(result.data)
});
})
app.listen(3001, ()=> {
console.log('App is running on port 3001');
})
Here I am trying to fetch data from mongodb using expressjs,
but in my browser nothing is coming.
No data is coming. But in my database there are documents.
Please have a look
const url = "mongodb://localhost:27017/recart";
Do you really need to provide collection's name here?
You can try:
const mongoUrl = 'mongodb://localhost:27017/'
const MongoClient = require('mongodb').MongoClient
app.get('/', async (req, res) => {
const client = await MongoClient.connect(mongoUrl, {
useNewUrlParser: true
})
const db = client.db("database_name")
const data = await db.collection("collection_name").find().toArray()
res.json(data)
}),