node.js 无法通过浏览器访问 mongodb - 看起来您正在尝试通过本机驱动程序端口上的 HTTP 访问 MongoDB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23446876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:16:04  来源:igfitidea点击:

Cannot access mongodb through browser - It looks like you are trying to access MongoDB over HTTP on the native driver port

node.jsmongodbexpress

提问by Michael

I open 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:

它打开 mongo shell 并提示输入 mongo 命令,但是当我转到 localhost/27017 时,我收到以下消息:

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 POSTdata 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.

我使用 express 创建了一个简单的 nodejs 应用程序,当我提供POST数据时,mongodb 似乎挂了。这是我在启动快速应用程序的终端中收到的消息,页面从不发布数据。所以我相信问题出在 mongo 内部,但我无法弄清楚。

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

应用程序.js

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

回答by David

Before Mongo 3.6:

在 Mongo 3.6 之前

You may start mongodb with

你可以从 mongodb 开始

mongod --httpinterface

And access it on

并访问它

http://localhost:28017

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

从 2.6 版开始:MongoDB 默认禁用 HTTP 接口。

Update

更新

HTTP Interface and REST API

MongoDB 3.6 removes the deprecated HTTP interface and REST API to MongoDB.

HTTP 接口和 REST API

MongoDB 3.6 删除了已弃用的 HTTP 接口和 REST API 到 MongoDB。

See Mongo http interface and rest api

Mongo http接口和rest api

回答by WiredPrairie

MongoDB has a simple web based administrative port at 28017 by default.

默认情况下,MongoDB 在 28017 有一个简单的基于 Web 的管理端口。

There is no HTTP access at the default port of 27017 (which is what the error message is trying to suggest). The default port is used for native driver access, not HTTP traffic.

默认端口 27017 上没有 HTTP 访问(这是错误消息试图建议的)。默认端口用于本地驱动程序访问,而不是 HTTP 流量。

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

要访问 MongoDB,您需要使用驱动程序,例如NodeJSMongoDB 本机驱动程序。您不会直接“发布”到 MongoDB(但您可以使用使用本机驱动程序的 express 创建一个 RESTful API)。相反,您将使用一个包装库来方便访问 MongoDB。您还可以考虑使用Mongoose(使用本机驱动程序),它在 NodeJS 中为 MongoDB 添加了一个类似 ORM 的模型。

If you can't get to the web interface, it may be disabled. Normally, I wouldn't expect that you'd need it for doing development unless you're checking logs and such.

如果您无法访问Web 界面,则它可能已被禁用。通常,除非您正在检查日志等,否则我不希望您需要它来进行开发。

回答by nekperu15739

Like the previous comment mention, the message "It looks like you are trying to access MongoDB over HTTP on the native driver port." its a warning because you are missunderstanding this line: mongoose.connect('mongodb://localhost/info'); and browsing this url: http://localhost:28017/

就像之前提到的评论一样,消息“看起来您正在尝试通过本地驱动程序端口上的 HTTP 访问 MongoDB。” 这是一个警告,因为您误解了这一行: mongoose.connect('mongodb://localhost/info'); 并浏览此网址: http://localhost:28017/

However, if you want to see the mongo's admin web page, you could do it, with this command:

但是,如果您想查看 mongo 的管理网页,可以使用以下命令:

mongod --rest --httpinterface

mongod --rest --httpinterface

browsing this url: http://localhost:28017/

浏览这个网址: http://localhost:28017/

the parameter httpinterface activate the admin web page, and the parameter rest its needed for activate the rest services the page require

参数 httpinterface 激活管理网页,参数 rest 是激活页面所需的其余服务所需的参数

回答by Coffee_fan

I am in ubuntu 14.04 and mongod is registered as an upstart service. The answers in this post sent me in the right direction. The only adaptation I had to do to make things work with the upstart service was to edit /etc/mongod.conf. Look for the lines that read:

我在 ubuntu 14.04 并且 mongod 注册为新贵服务。这篇文章中的答案让我朝着正确的方向前进。为了使 upstart 服务能够正常工作,我必须做的唯一调整是编辑/etc/mongod.conf. 寻找以下几行:

# Enable the HTTP interface (Defaults to port 28017).
# httpinterface = true

Just remove the #in front of httpinterface and restart the mongod service:

只需删除#httpinterface前面的并重新启动mongod服务:

sudo restart mongod

Note: You can also enable the rest interface by adding a line that reads rest=trueright below the httpinterface line mentioned above.

注意:您还可以通过rest=true在上面提到的 httpinterface 行正下方添加一行来启用 rest 接口。

回答by Prab

HTTP interface for MongoDB Deprecated since version 3.2 :)

MongoDB 的 HTTP 接口自 3.2 版起已弃用 :)

Check Mongo Docs: HTTP Status Interface

检查 Mongo 文档:HTTP 状态接口