[英]NodeJS express - Cannot GET route
我正在使用express和mongoose创建一个基本的API。 现在,我需要2个GET URL-一个用于从数据库中获取所有条目,另一个用于获取随机条目。 现在,第一个定义的路由- '/API/getWords'
工作正常,但是当我将浏览器导航到第二个路由'/API/getWords'
'API/getRandomWord'
,出现以下错误:
无法获取/ 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) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.