我无法向/ questions / ID / answers 404错误发出POST请求

时间:2016-11-02 08:58:37

标签: node.js express mongoose

所以我正在创建一个API来创建问题并获得问题ID的答案,但每当我使用邮递员创建一个POST请求来答案时。

每当我向 http://localhost:3000/api/questions/ID/answers 创建POST请求时,它都会给我一个404.我使用node-restful包来创建此API,这里是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);

如果有任何错误或者您希望查看更多代码,请告诉我们!

这是我的路线

'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;

2 个答案:

答案 0 :(得分:0)

我认为你应该添加:id/answers之类的

Question.register(router, '/questions/:id/answers');

答案 1 :(得分:0)

根据node-restful的文档,您需要打开详细信息:在自定义路由中为true,如:

            <div id = "pieces">
                <style>
                    .pieces 
                    {
                        width: 100%;
                        height: 100%;
                        float: left;
                        white-space: nowrap;
                        overflow: auto;
                        margin-left: auto;
                        margin-right: auto;
                        
                    }
                </style>
            <div id = "xMark" class = "x">
                X
                
                <script>
                    
                    window.addEventListener("resize", function dimensions() {
                        var textWidth = (window.innerWidth/3);
                        var textHeight = (window.innerHeight/3);
                        document.getElementById("xMark").style.height = textHeight+"px";
                        document.getElementById("xMark").style.width = textWidth+"px";
                    });
                    
                    //document._intervalId = setInterval(dimensions, 1);
                </script>
                <style>
                    .x
                    {
                        
                        
                        height: 33.333333333333vh;
                        display: inline-block;
                        
                        width:  33.333333333333vw;
                        background-color: lightblue;
                        font-size: 30px;
                        border: 5px solid black;
                        text-align: center;
                        vertical-align: middle;
                        overflow: auto;
                        
                        
                        
                    }
                    .x:hover
                    {
                        background-color: blue;
                        vertical-align: middle;
                        text-align: center;
                        cursor: pointer;
                        overflow: auto;
                    }
                </style>
            </div>
            <div id = "hyphonMark" class = "hyphon">
                --
                
                <script>
                    
                    window.addEventListener("resize", function dimensions() {
                        var textWidth = (window.innerWidth/3);
                        var textHeight = (window.innerHeight/3);
                        document.getElementById("hyphonMark").style.height = textHeight+"px";
                        document.getElementById("hyphonMark").style.width = textWidth+"px";
                    });
                    //document._intervalId = setInterval(dimensions, 1);
                    
                </script>
                <style>
                    .hyphon
                    {
                        
                        height: 33.333333333333vh;
                        display: inline-block;
                        width:  33.333333333333vw;
                        background-color: lightblue;
                        font-size: 30px;
                        border: 5px solid black;
                        text-align: center;
                        vertical-align: middle;
                        overflow: auto;
                        
                        
                        
                        
                    }
                    .hyphon:hover
                    {
                        background-color: blue;
                        vertical-align: middle;
                        text-align: center;
                        cursor: pointer;
                    }
                </style>
            </div>
            <div id = "oMark" class = "O">
                O
                
                <script>
                    
                    function dimensions() {
                        var textWidth = (window.innerWidth/3);
                        var textHeight = (window.innerHeight/3);
                        document.getElementById("oMark").style.height = textHeight+"px";
                        document.getElementById("oMark").style.width = textWidth+"px";
                    }
                    document.getElementById("oMark").addEventListener("resize", dimensions);
                    //document._intervalId = setInterval(dimensions, 1);
                    
                </script>
                <style>
                    .O
                    {
                        
                        height: 33.333333333333vh;
                        display: inline-block;
                        
                        width: 32.333333333333vw;
                        background-color: lightblue;
                        font-size: 30px;
                        border: 5px solid black;
                        text-align: center;
                        vertical-align: middle;
                        overflow: auto;
                        
                       
                        
                    }
                    .O:hover
                    {
                        background-color: blue;
                        vertical-align: middle;
                        text-align: center;
                        cursor: pointer;
                    }
                </style>
            </div>
            
            </div>
        </div>

});

这是完全未经测试的,并且可能不完全正确,因为您的模型很复杂且所有示例都非常简单。但你肯定需要一个自定义路线。这个小代码片段可能会设置一堆您不想要的路线,但它应该指向正确的方向。

我刚看了一下这里的文档: http://www.baugarten.me/node-restful/