我正在使用express和mongoose创建一个基本的API。现在我想要 2 个 GET URL - 一个用于从数据库获取所有条目,另一个用于获取随机条目。现在,首先定义的路线 - '/API/getWords'
工作正常,但是当我将浏览器导航到第二条路线 - 'API/getRandomWord'
时,出现以下错误:
Cannot GET /API/getRandomWord
现在我不知道我做错了什么。我可以只定义一个app.get,现在需要定义定义不同功能的参数吗?或者这是“正确”的方法吗?
感谢您的帮助。
我的 api.js:
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var Words = require('./models/words.js');
//initialize our express app
var app = express();
//use body parser with JSON
app.use(bodyParser.json());
//middleware for CORS requests
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
//addWords endpoint
app.post('/API/addWords', function(req, res) {
//get user from request body
var words = req.body;
var newWords = new Words({
quote: words.quote,
source: words.source,
author: words.author
});
newWords.save(function(err) {
if (err) {
if (err.code == 11000) {
res.status(400).send({
message: 'This quote already exists!'
});
} else {
res.status(500).send({
message: err.errmsg
});
}
} else {
console.log('quote saved!');
//if all goes well, send 200 + validation message
res.status(200).send({
message: 'Successfully quoted!'
});
}
});
});
//get all the words
app.get('/API/getWords', function(req, res) {
Words.find({}, function(err, words) {
if (err) {
res.status(500).send({
message: err.errmsg
});
} else {
res.status(200).send(words);
}
});
});
//get a random word
app.get('API/getRandomWord', function(req, res) {
//count ann the entries
Words.count().exec(function(err, count) {
//get a random number that is less or equal number of entries
var random = Math.floor(Math.random() * count);
//skip the random number, then return 1 entry
Words.findOne().skip(random).exec(
function(err, words) {
if (err) {
res.status(500).send({
message: err.errmsg
});
} else {
res.status(200).send(words);
}
});
});
});
//connect to mongoDB
mongoose.connect('');
//define our server
var server = app.listen(3000, function() {
console.log('api listening on ', server.address().port);
});
我的模型(words.js):
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// create a schema
var wordsSchema = new Schema({
author: {
type: String,
default: 'unknown',
index: true
},
source: {
type: String,
default: 'unknown',
index: true
},
quote: {
type: String,
unique: true,
required: true
},
created: Date,
updated: Date
});
//create the date
wordsSchema.pre('save', function(next) {
//get current date
var currentDate = new Date();
//assign date
this.updated = currentDate;
if (!this.created) {
this.created = currentDate;
}
next();
});
var Words = mongoose.model('Words', wordsSchema);
// make this available to our users in our Node applications
module.exports = Words;
最佳答案
您的 /API/getRandomWord
路由缺少前导 /
,因此需要是:
app.get('/API/getRandomWord', function(req, res) {
而不是:
app.get('API/getRandomWord', function(req, res) {
关于javascript - NodeJS Express - 无法获取路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35996328/