简体   繁体   English

Javascript:REST API Mongodb无法在Postman上连接

[英]Javascript: REST API Mongodb not connecting on Postman

I'm new to JavaScript and I'm trying to learn how to build REST API's from a local server on my computer. 我是JavaScript的新手,正在尝试学习如何从计算机上的本地服务器构建REST API。 When I test the GET request on Postman, however, it always comes back with an error: Could Not Get Any Response. 但是,当我在Postman上测试GET请求时,它总是返回一个错误:无法获得任何响应。

The purpose of the REST API is to (a) Store basic information about the planets in the solar system, (b) allow GET requests and (c) allow me to use POST to add more entries in the database. REST API的目的是(a)存储有关太阳系中行星的基本信息,(b)允许GET请求,(c)允许我使用POST在数据库中添加更多条目。

When I go to postman I use the following URL: localhost:8000/planets 当我去邮递员时,我使用以下URL: localhost:8000/planets

I'm using mongodb and mongoose. 我正在使用mongodb和mongoose。 I assume the problem is somewhere in my code because I'm not getting a response from the Server but I can't pinpoint where the problem is. 我认为问题出在我的代码中,因为我没有收到服务器的响应,但我无法查明问题出在哪里。 Thanks. 谢谢。

    var express = require('express');
var bodyParser = require('body-parser');
var _ = require('lodash');
var mongoose = require('mongoose');

var app = express();

app.use(express.static('planet_pages'));

app.use(bodyParser.urlencoded({extended: false}));

app.use(bodyParser.json);

mongoose.Promise = global.Promise;
var promise = mongoose.connect('mongodb://localhost/planet_server', {
  useMongoClient: true,
});

promise.then(function(db){
  console.log('DATABASE CONNECTED!!');
}).catch(function(err){
  console.log('CONNECTION ERROR', err);
});

var Schema = mongoose.Schema;

var planetSchema = new Schema({
  name: String, 
  position: String, 
  moons: String,
  diameterInKm: String,
  daysInYear: String,
  temperature: String 
})

var Planet = mongoose.model('Planet', planetSchema);
Planet.collection.drop();

//*****RAW DATA*****

var planets = [{
  name: "Mercury",
  position: "Mercury is the first planet from the Sun",
  moons: "Mercury has no moons",
  diameterInKm: "Mercury's diameter is 4,879km",
  daysInYear: "A year on Mercury lasts just 88 days!",
  temperature: "The temperature on Mercury is a scorching 427C"},

  { name: "Venus",
    position: "Venus is the second planet from the Sun",
    moons: "Venus has no moons",
    diameterInKm: "Venus's diameter is 12,104km",
    daysInYear: "A year on Mercury lasts 225 days!",
    temperature: "The temperature on Mercury is 467C. Hot enough to melt lead!"
}];

//*****END OF RAW DATA*****

app.get('/planets', function(req, res){
  Planet.find({}).exec(function(err, planets){
    if(err) {
      return res.status(500).send(err);
    }
    return res.json(planets);
  });
});

app.listen(8000, function(){
  console.log('I am listening');
});

EDIT: in the line app.use(express.static('planet_pages') , planet_pages is a folder inside my project folder which contains a couple of HTML pages that I hope to use to make AJAX queries. 编辑:在app.use(express.static('planet_pages') ,planet_pages是我的项目文件夹中的一个文件夹,其中包含几个HTML页面,我希望这些页面可以用来进行AJAX查询。

Also, supplementary question, in the line app.get('/planets', function(req, res) I'm using /planets as the extension because that's the name of the URL that holds my data in the //*****RAW DATA section. I got this from following a tutorial but I'm not sure why I'm supposed to do it. If anyone could explain this I'd MASSIVELY appreciate it. Thanks. 另外,在app.get('/planets', function(req, res)行中,还有一个补充问题,我使用/planets作为扩展名,因为这是将数据保存在// ***中的URL的名称**“原始数据”部分。我是从教程中获得的,但是我不确定为什么要这样做。如果有人能解释一下,我将非常感谢。

Also I was advised to use the Planet.collection.drop() to ensure the database refreshes properly when I re-load or make a change to the database. 还建议我使用Planet.collection.drop()来确保在我重新加载或更改数据库时数据库正确刷新。 Since I can't even make the GET request work, I'm not sure if I need this line. 由于我什至无法使GET请求正常工作,因此不确定是否需要此行。

Also, I've not put the POST request code in yet because I don't see the point until I can get the GET request function working. 另外,我还没有放入POST请求代码,因为直到让GET请求功能正常工作,我才意识到这一点。

尝试注释掉app.use(bodyParser.json)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM