简体   繁体   English

Node JS&MongoDB-发布和获取iOS服务器API的图像

[英]Node JS & MongoDB - Post and Get images for iOS server api

I am doing a very basic server for an app I am working on to help me understand node js and mongodb as a server for my mobile app. 我正在为一个正在使用的应用程序做一个非常基本的服务器,以帮助我理解node js和mongodb作为移动应用程序的服务器。 I have followed a tutorial and got it working posting simple todos. 我遵循了一个教程,并通过发布简单的待办事项使其工作。 But what I want to do next is post images along with the text in the POST request and retrieve them with the GET request as JSON. 但是我接下来要做的是将图像与POST请求中的文本一起发布,并使用GET请求作为JSON检索它们。 Below is the code that I have so far which successfully posts the todo name along with the created at date to the db. 下面是到目前为止我成功地将待办事项名称和创建日期一起发布到数据库的代码。

The below class is what I had for the Schema but I attempted to add how I thought an image would be added, but I do not think this is correct. 下面的类是我对Schema所拥有的,但是我尝试添加我认为将添加图像的方式,但是我认为这是不正确的。 I believe the correct practice would be to store the images in a file system for example https://{mydomain}/images/ and then when posting the image data, store the image in the folder and the url to the image in the database? 我相信正确的做法是将图像存储在文件系统中,例如https://{mydomain}/images/ ,然后在发布图像数据时,将图像存储在文件夹中,并将URL指向数据库中的图像?

todoListModel.js todoListModel.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);

todoListController.js todoListController.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' });
  });
};

todoListRoutes.js todoListRoutes.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 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);

If someone could shed some light on this it would be greatly appreciated. 如果有人可以对此有所了解,将不胜感激。

I think that would be the best approach first upload the image into your project folder or you can upload the image in AWS S3 bucket if you are using, and then store the path of that image into your collection. 我认为这是最好的方法,首先将图像上传到项目文件夹中,或者如果正在使用,则可以将图像上传到AWS S3存储桶中,然后将该图像的路径存储到集合中。 Correct schema would be 正确的架构是

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
 }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM