简体   繁体   中英

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. When I test the GET request on Postman, however, it always comes back with an error: Could Not Get Any Response.

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.

When I go to postman I use the following URL: localhost:8000/planets

I'm using mongodb and 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.

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.

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. Since I can't even make the GET request work, I'm not sure if I need this line.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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