如何从按钮调用帖子请求?节点表达

时间:2018-06-09 08:23:21

标签: javascript node.js express angular5

如何从点击按钮调用控制器? 我尝试了邮递员它运作良好..但我不能把它称为我的前端按钮。

我正在使用nodemailer和node express来发送电子邮件..

这是我的代码..

这是对的吗? - 提前谢谢。

我正在弄清楚我的代码是否正确。

mailController.js

// mail controller routes
var express = require('express');
var router = express.Router();
var nodemailer  = require('nodemailer');

// get /api/mail/
router.get('/send-email', (req,res) => {
  res.send('GET response');
});

// post /api/mail/
router.route('/send-email').post((req,res) => {
  let transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        type: 'OAuth2', 
        user: 'dev.xxx@gmail.com',
        clientId: 'xxx-xxx.apps.googleusercontent.com',
        clientSecret: 'SY0-xxxx',
        refreshToken: '1/xxx',
        accessToken: 'ya29.xxx-xxx-xxx-xxx'
      }
  })

  let mailOptions = {
    from: 'FReyes <dev.xxx@gmail.com>',
    to: 'xxx@gmail.com',
    subject: 'Nodemailer test',
    text: 'Hello World!!'
  }

  transporter.sendMail(mailOptions, function (err, res) {
    if(err){
      console.log('Error');
    } else {
      console.log('Email Sent');
    }
  })
});

// put /api/mail/
router.put('/send-email',(req,res) => {
  res.send('PUT response');
});

// delete /api/mail/
router.delete('/send-email',(req,res) => {
  res.send('DELETE response');
});

module.exports = router;

server.js

    var express     = require('express');
    var bodyParser  = require('body-parser');
    var nodemailer  = require('nodemailer');

    var app         = express(); // Please do not remove this line, since CLI uses this line as guidance to import new controllers

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

    app.use(function(req, res, next) {
      // console.log(req);
      res.setHeader('Access-Control-Allow-Origin', '*');
      res.setHeader('Access-Control-Allow-Credentials', 'true');
      res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT,DELETE');
      res.setHeader('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers');

      //and remove cacheing so we get the most recent comments
      res.setHeader('Cache-Control', 'no-cache');
      next();
    });

    var mailController = require('./controllers/mailController');
    app.use('/api/mail', mailController);

    app.listen(process.env.PORT || 5015, () => {
      console.log('Server is running');
    });

contact.component.ts

  sendMail() {    
    const data = {
        to: 'xxx@gmail.com',
        subject: 'Subject',
        txt: 'hello world'
    };
    console.log('asd');
    return this.http.post('localhost:5015/api/mail/send-email', data).map((response: Response) =>{
      console.log (response.json());
    });
  }

HTML

<input type="button" value="test" (click)="sendMail(data)">

1 个答案:

答案 0 :(得分:0)

不要忘记在地图功能中添加协议名称和返回值

sendMail() {
  const data = {
    to: 'xxx@gmail.com',
    subject: 'Subject',
    txt: 'hello world'
  };
  console.log('asd');
  return this.http.post('http://localhost:5015/api/mail/send-email', data).subscribe(data => console.log(data));
}