未找到NodeJS路线GET:/ find / 123

时间:2019-10-24 16:03:22

标签: javascript node.js express routing node-modules

我正在尝试创建一个将从本地mongodb中获取数据的api

我添加了依赖项,创建了数据库模式和集合名称,并连接到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');

现在在用于查询数据的.js文件中添加了路由:

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

我遇到错误Route GET:/find/123 not found

1 个答案:

答案 0 :(得分: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());

然后您的app.get()有效

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

您需要使用bodyparser中间件来处理node.js中的router。希望对您有帮助。