Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I open the terminal and enter the following commands

sudo mongod

which then outputs

[initandlisten] waiting for connections on port 27017

I open another terminal and enter

sudo mongo

which open the mongo shell and prompts for mongo commands, but when I go to localhost/27017 I receive the following message:

It looks like you are trying to access MongoDB over HTTP on the native driver port.

I created a simple nodejs application using express and when I POST data it seems the mongodb gets hung up. This is the message which I receive in the terminal in which I start my express application and the page never posts the data. So I believe the problem lies within mongo but I cannot figure it out.

POST /info 200 120002ms

Here is my express code

var Info = require('../models/info'); 

var path = require('path'); 

var fs = require('fs'); 

var join = path.join; 

exports.form = function(req,res){ 

res.render('info', { 

myName: 'Michael' 

}); 

}; 

exports.submit = function(){ 

console.log('Within exports.submit 1'); 

return function(req,res,next){ 

console.log('Within exports.submit 2 '); 

var firstName = req.name.first; 

var lastName = req.name.last; 

Info.create({ 

firstName: firstName, 

lastName: lastName 

},function(err){ 

if(err) return next(err); 

res.redirect('/') 

}); 

};

Model

var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/info'); 

var schema = new mongoose.Schema({ 

firstName: String, 

lastName: String 

}); 

module.exports = mongoose.model('info',schema);

app.js

... 

app.get('/info',info.form); 

app.post('/info',info.submit); 

...

3 Answers

0 votes
by (106k points)

To access mongodb through a browser you should use the below-mentioned command:-

You may start mongodb with

mongod --httpinterface

And access it on

http://localhost:28017

Since version 2.6: MongoDB disables the HTTP interface by default.

0 votes
by (107k points)

MongoDB has an uncomplicated web-based central port at 28017 by default. At the default port of 27017, there is no HTTP access. The error port is used for native driver access, not HTTP traffic.

 

To obtain MongoDB, you'll need to use a driver like the MongoDB essential driver for NodeJS. You won't "POST" to MongoDB instantly (but you might create a RESTful API using express which uses the native drivers). Instead, you'll use a wrapper library that makes obtaining MongoDB convenient. You might also acknowledge using Mongoose (which uses the native driver) which adds an ORM-like model for MongoDB in NodeJS.

Cannot access mongodb through a browser - It looks like you are trying to access MongoDB over HTTP on the native driver port
Intellipaat-community
by
I too am getting this error.  I have a mongo-express container and a mongodb container (and a node.js container).  Everything works on my laptop if I use docker-compose with a mongo.yaml file that starts all 3 apps.  But if I load those containers in an azure app service (using az webapp create, it fails.  I commented out the node.js container, so it's just the two mongo containers, and it just gives me that message (no errors in any logs).  I was just expecting to query  my mongodb container's database using mongo-express.  works on laptop, fails in azure app service.
0 votes
by (3.1k points)

The error "It looks like you are trying to access MongoDB over HTTP on the native driver port" occurs when you try to access MongoDB through a web browser on port 27017. To resolve this, use a MongoDB client like MongoDB Compass or the MongoDB Shell with the command: 
mongo --host <hostname> --port 27017 
For a web-based interface, consider tools like MongoDB Atlas or Mongo-Express. Ensure your connection string is correctly formatted if connecting from an application. MongoDB is not accessible via HTTP directly. 

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...