Re-use POST and GET methods in Express

Larsmanson

I'm building a CMS where I have to do a lot of POST and GET requests to my MongoDB. The problem I am having now is that, the more I work on my CMS, the more POST and GET requests I have to do and eventually a lot of double code I'm having in my application. My question is, can I somehow re-use the POST and GET methods? I am using Express framework, MongoDB and Angular on the Front-End.

Here is an example how my application looks like:

Express:

router.post('/news_blocks', function(req, res, next){
  var randomNumber = Math.floor(1000 + Math.random() * 9000);
  var news_image = req.files.myImage;
  news_image.mv('/home//projects/website/public/uploads/' + 'image_' + randomNumber + '.jpg' , function(err) {
    if(err){
      console.log(err);
    }else{
      var data = new news_blocks(postOptions(req, randomNumber));
      saveToDB(data,res);
     }
  });
});

router.post('/research', function(req, res, next){
  var randomNumber = Math.floor(1000 + Math.random() * 9000);
  var research_image = req.files.myImage;
  research_image.mv('/home/projects/website/public/uploads/' + 'image_' + randomNumber + '.jpg' , function(err) {
    if(err){
      console.log(err);
    }else{
      var data = new research_blocks(postOptions(req, randomNumber));
      saveToDB(data,res);
     }
  });
});

postOptions = function(req, randomNumber){
  var options = {
    title: req.body.title,
    date: new Date,
    message: req.body.message,
    image: 'image_' + randomNumber
  };
  return options;
};

MongoDB model:

file1:

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

var research_block = new mongoose.Schema({
  title: String,
  date: String,
  message: String,
  image: String
}, {collection: 'research'});

module.exports = mongoose.model("research", research_block);

File 2:

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

var news_block = new mongoose.Schema({
  title: String,
  date: String,
  message: String,
  image: String
}, {collection: 'news'});

module.exports = mongoose.model("news", news_block);

As you can see, there is a lot of the same code in the POST methods. But I am not sure how I make this more DRY

Prasanta Bose

Suggestion 1:
Make a controller file for every table. And, write functions in it to do all kind of operations in it. And, while calling a service send type of operation along with it. On basis of type you can call any function you want.
Suggestion 2:
Try using GraphQL

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Combine GET and POST request methods in Spring

From Dev

Documentation for Flask app object `get` and `post` class methods?

From Dev

How to get a list of java restful GET/POST methods?

From Dev

WCF REST Error 401.3 in PUT and DELETE methods but not in GET and POST

From Dev

Should one use GET/POST or specific methods for class based views in Django?

From Dev

Get form data for both POST and GET requests in Express

From Dev

Variable from post() to get_context_data() methods of TemplateView

From Dev

ServiceStack routing GET requests to POST methods

From Dev

Slim framework and GET/PUT/POST methods

From Dev

Using [Authorize] on the GET and POST methods with same name

From Dev

Get and Post methods in Python (Flask)

From Dev

How to use Mongoose save method in Express POST route

From Dev

Why a browser only supports GET and POST HTTP methods?

From Dev

Django distinguish between get / post in view's methods

From Dev

RequestMapping GET and POST methods handling in Spring REST

From Dev

User logout: Redirect GET to POST (Node/Express)

From Dev

trying to use POST, but API tries to use GET

From Dev

Cannot GET /POST? error in express?

From Dev

Is it possible to have 2 methods (GET and POST) with the same route?

From Dev

MySQL query works with POST but not with GET (Node/Express)

From Dev

How to GET/POST files in express.static()

From Dev

Using [Authorize] on the GET and POST methods with same name

From Dev

Use $_GET together with $_POST

From Dev

How to use Mongoose save method in Express POST route

From Dev

Which methods does curl use in these two cases for authentication, GET or POST?

From Dev

Cannot GET /POST? error in express?

From Dev

Cannot GET/POST with express Router()

From Dev

Glassfish allow only GET and POST methods

From Dev

Express can't use app.get

Related Related

  1. 1

    Combine GET and POST request methods in Spring

  2. 2

    Documentation for Flask app object `get` and `post` class methods?

  3. 3

    How to get a list of java restful GET/POST methods?

  4. 4

    WCF REST Error 401.3 in PUT and DELETE methods but not in GET and POST

  5. 5

    Should one use GET/POST or specific methods for class based views in Django?

  6. 6

    Get form data for both POST and GET requests in Express

  7. 7

    Variable from post() to get_context_data() methods of TemplateView

  8. 8

    ServiceStack routing GET requests to POST methods

  9. 9

    Slim framework and GET/PUT/POST methods

  10. 10

    Using [Authorize] on the GET and POST methods with same name

  11. 11

    Get and Post methods in Python (Flask)

  12. 12

    How to use Mongoose save method in Express POST route

  13. 13

    Why a browser only supports GET and POST HTTP methods?

  14. 14

    Django distinguish between get / post in view's methods

  15. 15

    RequestMapping GET and POST methods handling in Spring REST

  16. 16

    User logout: Redirect GET to POST (Node/Express)

  17. 17

    trying to use POST, but API tries to use GET

  18. 18

    Cannot GET /POST? error in express?

  19. 19

    Is it possible to have 2 methods (GET and POST) with the same route?

  20. 20

    MySQL query works with POST but not with GET (Node/Express)

  21. 21

    How to GET/POST files in express.static()

  22. 22

    Using [Authorize] on the GET and POST methods with same name

  23. 23

    Use $_GET together with $_POST

  24. 24

    How to use Mongoose save method in Express POST route

  25. 25

    Which methods does curl use in these two cases for authentication, GET or POST?

  26. 26

    Cannot GET /POST? error in express?

  27. 27

    Cannot GET/POST with express Router()

  28. 28

    Glassfish allow only GET and POST methods

  29. 29

    Express can't use app.get

HotTag

Archive