Axios post 请求将整个文件内容呈现为响应而不是执行服务器端文件
Axios post request renders entire file contents as response instead of executing serverside file
我正在尝试在 React 应用程序中使用 nodemmailer 和 axios 发送 gmail。但是我得到的响应是整个文件的内容。任何帮助是极大的赞赏。谢谢。
这是我的前端 axios post 请求。此请求工作正常,返回 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: 'info@gmail.com',
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: 'sender@gmail.com', //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'
;
问题是您已将 mail.js
文件添加到 public 目录。
app.use('/static', express.static(path.join(__dirname, 'public')))
因此,当您点击 https://localhost/mail
时,它正在解析 public 中匹配的任何文件并提供内容。
确保您没有将它添加到 public 目录中。或用于添加客户端代码的任何其他目录。
我正在尝试在 React 应用程序中使用 nodemmailer 和 axios 发送 gmail。但是我得到的响应是整个文件的内容。任何帮助是极大的赞赏。谢谢。
这是我的前端 axios post 请求。此请求工作正常,返回 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: 'info@gmail.com',
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: 'sender@gmail.com', //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'
;
问题是您已将 mail.js
文件添加到 public 目录。
app.use('/static', express.static(path.join(__dirname, 'public')))
因此,当您点击 https://localhost/mail
时,它正在解析 public 中匹配的任何文件并提供内容。
确保您没有将它添加到 public 目录中。或用于添加客户端代码的任何其他目录。