Node.js Rest API调用

时间:2016-01-08 23:37:44

标签: node.js rest express

我有一个需要帮助的后API请求。我想在Post请求之后调用一个函数,但该函数没有被执行。

var restful = require('node-restful');
module.exports = function(app, route) {
  // Setup the controller for REST.
  var rest = restful.model(
    'post',
    app.models.post
  ).methods(['get', 'put', 'post', 'delete']);

  // Register this endpoint with the application.
  rest.register(app, route);
  rest.after('post', send_me_email)

  function send_me_email(req, res, next) {
    server.send({
     text:    "i hope this works", 
     from:    "****@******.com", 
     to:      "******@gmail.com",
     subject: "testing emailjs"
    }, 
    function(err, message) { 
      console.log(err || message); 
    })

    console.log("HERE IS THE POST AFTER: " + req.body);
    next();
  }

  // Return middleware.
  return function(req, res, next) {
    next();
  };
}

1 个答案:

答案 0 :(得分:1)

    function send_me_email(req, res, next) {
        server.send({
         text:    "i hope this works", 
         from:    "****@******.com", 
         to:      "******@gmail.com",
         subject: "testing emailjs"
        }, 
        function(err, message) { 
          console.log(err || message); 
        })

        console.log("HERE IS THE POST AFTER: " + req.body);
        next();
      }

应该是

function send_me_email(req, res, next) {
    server.send({
     text:    "i hope this works", 
     from:    "****@******.com", 
     to:      "******@gmail.com",
     subject: "testing emailjs"
    }, 
    function(err, message) { 
      console.log(err || message);

       if(err) return next(err);

       console.log("HERE IS THE POST AFTER: " + req.body);
       next();

    })

  }