0

I am trying to create an api which would fetch data from local mongodb

I have added dependencies , created the database schema and the name of the collection, and connected to MongoDB:

var cors = require('cors');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var app = express();

var supportSchema = new Schema({
    _id: {type:ObjectId, auto: true },
    UserId: { type:String, required: true },
    Name: String
}, {
    collection: 'abc'
});

var SupportModel = mongoose.model('Model', supportSchema);
mongoose.connect('mongodb://localhost:27017/dbName');

Now added routes in the same .js file that we will use to query the data:

app.get('/find/:query', function(req, res) {
    let envId = request.params.envId;
    SupportModel.find({environmentId: envId}, {}, function(err) {
        if (err) {
            console.log(err);
        }
    })
})

I am getting an error Route GET:/find/123 not found

Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
meallhour
  • 13,921
  • 21
  • 60
  • 117

1 Answers1

0
let express = require('express');
let bodyParser = require('body-parser');
let cors = require('cors');
let router = express.Router();
let app = express();

let http = require('http');
let server = http.Server(app);

app.use(bodyParser.json());  
use with existing express server.
app.use (cors());

Then your app.get() works

app.get('/', (req, res) => {  } ):

You need to use body- parser middle ware to working with router in node.js . Hope this helps you.

Developer
  • 3,309
  • 2
  • 19
  • 23