Axios发布请求将整个文件内容呈现为响应,而不是执行服务器端文件

问题描述 投票:0回答:1

我正在尝试在React应用程序中使用nodemmailer和axios发送gmail。但是我得到的响应是整个文件的内容。任何帮助是极大的赞赏。谢谢。

这是我的前端axios发布请求。此请求可以正常工作,它可以重现200状态代码。问题出在响应上。

const axios = require('axios');
 axios({
            method: "POST", 
            url:"https://localhost/mail", 
            data: {
                recepients: recepients,   
                msgbody: msgbody,  
                msgsubject: msgsubject
            }
        }).then((response)=>{
            if (response.data.msg === 'success'){
                console.log("Message Sent."); 
               // this.resetForm()
            }else if(response.data.msg === 'fail'){
                console.log("Message failed to send.")
            }
        });

这是我的mail.js文件。

    const express = require('express')
    const app = express();
    var nodemailer = require('nodemailer');

    var transport = {
      host: 'smtp.gmail.com',
      port: 587,
      domain:'gmail.com',
      secure:false,
      auth: {
        user: '[email protected]',
        pass: 'password'
      }
    }

    var transporter = nodemailer.createTransport(transport)

    transporter.verify((error, success) => {
      if (error) {
        console.log(error);
      } else {
        console.log('Server is ready to take messages');
      }
    });


    app.post('/mail', (req, res, next) => {
      var name = "sender";
      var email = req.body.email;
      var message = req.body.message;

      var mail = {
        from: name,
        to: '[email protected]',  //Change to email address that you want to receive messages on
        subject: 'New Message from Contact Form',
        text: 'hi'
      }

      transporter.sendMail(mail, (err, data) => {
        if (err) {
          res.json({msg: 'fail'});
        } else {
          res.json({msg: 'success'});
        }
      }

)
});

此文件未执行。我得到了相同的文件内容作为响应。任何人都可以帮助解决这个问题。预先感谢。

这是我的app.js文件。

/ *特定于应用程序的逻辑* /

import 'jquery';
import 'jquery-contextmenu';
import 'jQuery-Impromptu';

import conference from './conference';
import API from './modules/API';
import keyboardshortcut from './modules/keyboardshortcut/keyboardshortcut';
import remoteControl from './modules/remotecontrol/RemoteControl';
import translation from './modules/translation/translation';
import UI from './modules/UI/UI';
import axios from 'axios';

window.APP = {
    API,
    conference,

    // Used by do_external_connect.js if we receive the attach data after
    // connect was already executed. status property can be 'initialized',
    // 'ready', or 'connecting'. We are interested in 'ready' status only which
    // means that connect was executed but we have to wait for the attach data.
    // In status 'ready' handler property will be set to a function that will
    // finish the connect process when the attach data or error is received.
    connect: {
        handler: null,
        status: 'initialized'
    },

    // Used for automated performance tests.
    connectionTimes: {
        'index.loaded': window.indexLoadedTime
    },

    keyboardshortcut,
    remoteControl,
    translation,
    UI
};

// TODO The execution of the mobile app starts from react/index.native.js.
// Similarly, the execution of the Web app should start from react/index.web.js
// for the sake of consistency and ease of understanding. Temporarily though
// because we are at the beginning of introducing React into the Web app, allow
// the execution of the Web app to start from app.js in order to reduce the
// complexity of the beginning step.
import './react'

;

node.js reactjs express nodemailer
1个回答
0
投票

问题是您已将mail.js文件添加到公共目录。

app.use('/static', express.static(path.join(__dirname, 'public')))

所以发生的事情是,当您点击https://localhost/mail时,它正在解析任何与公共内容和服务内容匹配的文件。

请确保您未将其添加到公共目录中。或用于添加客户端代码的任何其他目录。

© www.soinside.com 2019 - 2024. All rights reserved.