【问题标题】:Node JS & MongoDB - Post and Get images for iOS server apiNode JS & MongoDB - 发布和获取 iOS 服务器 api 的图像
【发布时间】:2017-10-19 15:18:46
【问题描述】:

我正在为我正在开发的应用程序做一个非常基本的服务器,以帮助我理解 node js 和 mongodb 作为我的移动应用程序的服务器。我遵循了一个教程,并让它发布了简单的待办事项。但是我接下来要做的是将图像与 POST 请求中的文本一起发布,并使用 GET 请求作为 JSON 检索它们。下面是我迄今为止成功将待办事项名称连同创建日期一起发布到数据库的代码。

下面的类是我为 Schema 所拥有的,但我试图添加我认为添加图像的方式,但我认为这是不正确的。我相信正确的做法是将图像存储在文件系统中,例如https://{mydomain}/images/,然后在发布图像数据时,将图像存储在文件夹中,并将图像的 url 存储在数据库中?

tod​​oListModel.js

'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;


var TaskSchema = new Schema({
  name: {
    type: String,
    required: 'Kindly enter the name of the task'
  },
  img: {
    data: Buffer,
    contentType: String
  },
  Created_date: {
    type: Date,
    default: Date.now
  }
});

module.exports = mongoose.model('Tasks', TaskSchema);

tod​​oListController.js

'use strict';


var mongoose = require('mongoose'),
  Task = mongoose.model('Tasks');

exports.list_all_tasks = function(req, res) {
  Task.find({}, function(err, task) {
    if (err)
      res.send(err);
    res.json({
      data: {
        task
      }
    });
  });
};




exports.create_a_task = function(req, res) {
  var new_task = new Task(req.body);
  new_task.save(function(err, task) {
    if (err)
      res.send(err);
    res.json({
      data: {
        task
      }
    });
  });
};


exports.read_a_task = function(req, res) {
  Task.findById(req.params.taskId, function(err, task) {
    if (err)
      res.send(err);
    res.json({
      data: {
        task
      }
    });
  });
};


exports.update_a_task = function(req, res) {
  Task.findOneAndUpdate({_id: req.params.taskId}, req.body, {new: true}, function(err, task) {
    if (err)
      res.send(err);
    res.json({
      data: {
        task
      }
    });
  });
};


exports.delete_a_task = function(req, res) {


  Task.remove({
    _id: req.params.taskId
  }, function(err, task) {
    if (err)
      res.send(err);
    res.json({ message: 'Task successfully deleted' });
  });
};

tod​​oListRoutes.js

'use strict';
module.exports = function(app) {
  var todoList = require('../controllers/todoListController');

  // todoList Routes
  app.route('/tasks')
    .get(todoList.list_all_tasks)
    .post(todoList.create_a_task);


  app.route('/tasks/:taskId')
    .get(todoList.read_a_task)
    .put(todoList.update_a_task)
    .delete(todoList.delete_a_task);
};

server.js

var express = require('express'),
  app = express(),
  port = process.env.PORT || 3000,
  mongoose = require('mongoose'),
  Task = require('./api/models/todoListModel'), //created model loading here
  bodyParser = require('body-parser');

// mongoose instance connection url connection
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/Tododb');


app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());


var routes = require('./api/routes/todoListRoutes'); //importing route
routes(app); //register the route


app.listen(port);


console.log('todo list RESTful API server started on: ' + port);

如果有人能对此有所了解,将不胜感激。

【问题讨论】:

    标签: javascript ios node.js mongodb


    【解决方案1】:

    我认为这是最好的方法,首先将图像上传到您的项目文件夹中,或者如果您正在使用,您可以将图像上传到 AWS S3 存储桶中,然后将该图像的路径存储到您的集合中。 正确的架构是

    var TaskSchema = new Schema({
     name: {
      type: String,
      required: 'Kindly enter the name of the task'
     },
     img: {
      type: String,
     },
     Created_date: {
      type: Date,
      default: Date.now
     }
    });
    

    【讨论】:

    猜你喜欢
    • 2012-08-15
    • 2011-11-06
    • 2017-11-02
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 2021-08-04
    • 2016-11-10
    相关资源
    最近更新 更多