Search code examples
expressterminallocalhostdeprecated

Express Deprecated


I have a photo app that uploads photos to AWS. When testing the uploading photos feature on my localhost, my terminal throws the following error:

express deprecated res.send(status, body): Use res.status(status).send(body) instead aws/aws.js:50:18

My photos DO save to AWS, im just wondering what this error is and how to fix it. Below is my aws code that the error refers too.

'use strict';

var AWS = require('aws-sdk'),
    crypto = require('crypto'),
    config = require('./aws.json'),
    createS3Policy,
    getExpiryTime;

getExpiryTime = function () {
  var _date = new Date();
  return '' + (_date.getFullYear()) + '-' + (_date.getMonth() + 1) + '-' +
    (_date.getDate() + 1) + 'T' + (_date.getHours() + 3) + ':' + '00:00.000Z';
};

createS3Policy = function(contentType, callback) {
  var date = new Date();
  var s3Policy = {
    'expiration': getExpiryTime(),
    'conditions': [
      ['starts-with', '$key', 'images/'],
      {'bucket': config.bucket},
      {'acl': 'public-read'},
      ['starts-with', '$Content-Type', contentType],
      {'success_action_status' : '201'}
    ]
  };

  // stringify and encode the policy
  var stringPolicy = JSON.stringify(s3Policy);
  var base64Policy = new Buffer(stringPolicy, 'utf-8').toString('base64');

  // sign the base64 encoded policy
  var signature = crypto.createHmac('sha1', config.secretAccessKey)
  .update(new Buffer(base64Policy, 'utf-8')).digest('base64');

  // build the results object
  var s3Credentials = {
    s3Policy: base64Policy,
    s3Signature: signature,
    AWSAccessKeyId: config.accessKeyId
  };

  // send it back
  callback(s3Credentials);
};

exports.getS3Policy = function(req, res) {
  createS3Policy(req.query.mimeType, function (creds, err) {
    if (!err) {
      return res.send(200, creds);
    } else {
      return res.send(500, err);
    }
  });
};

Solution

  • Replace res.send(statusCode, "something") with res.status(statusCode).send("something")

    This should do it for your code:

    exports.getS3Policy = function (req, res) {
      createS3Policy(req.query.mimeType, function (creds, err) {
        if (!err) {
          return res.send(creds); //200 is not needed here, express will default to this
        } else {
          return res.status(500).send(err);
        }
      });
    };