so I am making an API for creating questions and getting answers on the question id but whenever I use postman to create a POST request to answers.
Whenever I create a POST request to http://localhost:3000/api/questions/ID/answers it gives me a 404. I use node-restful package for creating this API and here is the Schema
// Dependencies
var restful = require('node-restful');
// Database
var mongoose = restful.mongoose;
var Schema = mongoose.Schema;
// Question Schema
var QuestionSchema = new Schema({
qTitle: {
type: String,
required: true
},
qBody: {
type: String,
required: true
},
created_at: {
type: Date
},
updated_at: {
type: Date
},
answers: [{
aTitle: {
type: String,
required: true
},
aBody: {
type: String,
required: true
},
created_at: Date,
updated_at: Date
}]
});
// Export the question schema
module.exports = restful.model('Questions', QuestionSchema);
If there is any errors or if you wish to see more code let me know!
Here is my routes
'use strict';
var express = require('express');
var router = express.Router();
var Question = require('../models/question');
Question.methods(['get', 'put', 'post', 'delete']);
Question.register(router, '/questions');
// Exports the router
module.exports = router;
我认为您应该添加:id/answers
类的
Question.register(router, '/questions/:id/answers');
According to the documentation for node-restful you need to turn on detail: true in a custom route like:
Question.route('answers', {
method:'post',
detail: true,
handler: function(req, res, next) {
// req.params.id holds the resource's id
res.send("I'm at /questions/:id/answers")
}
});
this is totally untested, and likely not completely correct since your model is complex and all the examples were pretty simple. But you definitely need a custom route. This little code snippet probably will set up a bunch of routes you don't want as well, but it should point you in the right direction.
I just took a quick look at the documentation here: http://www.baugarten.me/node-restful/
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.