将json数据卷曲到localhost不会返回json

时间:2016-12-01 03:47:46

标签: node.js mongodb curl http-post mean-stack

我是MEAN堆栈的新手,并且我的项目存在一些问题。目标是库存管理员,但这并不重要。

我正在尝试通过将一些json数据卷曲到它来测试控制器,然后重新获取json数据。

我输入:

curl -H "Content-Type: application/json" -X POST -d '{"name":"foo","description":"bar"}' http://localhost:3000/categories

接着是

curl get http://localhost:3000/categories

返回

[{"_id":"583f94f2d419493b3b5287ba","__v":0},{"_id":"583f95b5d419493b3b5287bb","__v":0},{"_id":"583f96a2d419493b3b5287bc","__v":0},{"_id":"583f97d6d419493b3b5287bd","__v":0},{"_id":"583f9835d419493b3b5287be","__v":0},{"_id":"583f9846d419493b3b5287bf","__v":0},{"_id":"583f98c7d419493b3b5287c0","__v":0},{"_id":"583f98e1d419493b3b5287c1","__v":0},{"_id":"583f98ecd419493b3b5287c2","__v":0},{"_id":"583f9b1dd419493b3b5287c3","__v":0}]

我删除了html,这与此无关。我应该得到的不仅仅是__v和__id。有什么明显我做错了吗?

Routes.js代码:

'use strict';

module.exports = function(app) {
    var categories = require('../controllers/categories.server.controller');

    app.route('/categories')
      .get(categories.list)
      .post(categories.create);

    // the categoryId param is added to the params object for the request
   app.route('/categories/:categoryId')
    .get(categories.read);

    app.route('/categories/:categoryId')
        .get(categories.read)
        .put(categories.update)
        .delete(categories.delete);

    // Finish by binding the article middleware
    // What's this? Where the categoryId is present in the URL
    // the logic to 'get by id' is handled by this single function
    // and added to the request object i.e. request.category.
    app.param('categoryId', categories.categoryByID);
};

Categories.server.controller.js代码:

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    errorHandler = require('./errors.server.controller'),
    Category = mongoose.model('Category'),
    _ = require('lodash');

/**
 * Create a Category
 */
exports.create = function(req, res) {
    var category = new Category(req.body);

    category.save(function(err) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.status(201).json(category);
        }
    });
};

/**
 * Show the current Category
 */
exports.read = function(req, res) {
    res.json(req.category);
};

/**
 * Update a Category
 */
exports.update = function(req, res) {
    var category = req.category;

    category = _.extend(category, req.body);

    category.save(function(err) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.json(category);
        }
    });
};

/**
 * Delete an Category
 */
exports.delete = function(req, res) {
    var category = req.category;

    category.remove(function(err) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.json(category);
        }
    });
};

/**
 * List of Categories
 */
exports.list = function(req, res) {
    Category.find().sort('name').exec(function(err, categories) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.json(categories);
        }
    });
};

/**
 * Category middleware
 */
exports.categoryByID = function(req, res, next, id) {

    if (!mongoose.Types.ObjectId.isValid(id)) {
        return res.status(400).send({
            message: 'Category is invalid'
        });
    }

    Category.findById(id).exec(function(err, category) {
        if (err) return next(err);
        if (!category) {
            return res.status(404).send({
                message: 'Category not found'
            });
        }
        req.category = category;
        next();
    });
};

0 个答案:

没有答案