我正在尝试使用localhost:3000中的nodemailer将数据发送到邮件的表单运行良好但是当我在社区服务器上加载项目时,我无法使用此代码
项目根目录中的应用程序,名为nodeMensajeria / src / node modules,app.js和configMensaje.js
mira la raiz de mi proyecto app.js
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const configMensaje = require('./configMensaje');
const app = express();
app.use(bodyParser.json());
app.use(cors())
app.post('/formulario', (req, res) => {
configMensaje(req.body);
res.status(200).send();
})
app.listen(3000, () => {
console.log('Servidor corriendo')
});
configMensaje.js
const nodemailer = require('nodemailer');
module.exports = (formulario) => {
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'correoorigen',
pass: 'contraseña'
}
});
const mailOptions = {
from: `"${formulario.nombre} 👻" <${formulario.email}>`,
to: 'correodestino', //
subject: formulario.asunto,
html: `
<strong>Nombre:</strong> ${formulario.nombre} <br/>
<strong>Asunto:</strong> ${formulario.asunto} <br/>
<strong>E-mail:</strong> ${formulario.email} <br/>
<strong>Número de contacto:</strong> ${formulario.contacto} <br/>
<strong>Mensaje:</strong> ${formulario.mensaje}
`
};
transporter.sendMail(mailOptions, function (err, info) {
if (err)
console.log(err)
else
console.log(info);
});
}
服务message.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
@Injectable({
providedIn: 'root'
})
export class MessageService {
constructor(private _http: HttpClient) { }
sendMessage(body) {
return this._http.post('http://107.180.59.131:3000/formulario', body);
}
}
我正在使用我的域名的IP
app.component.ts
import { Component, OnInit } from '@angular/core';
import { MessageService } from '../services/message.service';
import swal from 'sweetalert';
import { VirtualTimeScheduler } from 'rxjs';
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.scss']
})
export class FormComponent implements OnInit {
constructor(public _MessageService: MessageService) { }
contactForm(form) {
this._MessageService.sendMessage(form).subscribe(() => {
swal("Formulario de contacto", "Mensaje enviado correctamente", 'success');
});
}
ngOnInit() {
}
}
It shows me that the app is executed
但是在生产中发送表单时,它会显示以下错误
无法加载资源:net :: ERR_CONNECTION_TIMED_OUT main.5cb5f6b8477568c35bd7.js:1 ERROR e {headers:t,status:0,statusText:“Unknown Error”,url:“http://107.180.59.131:3000/formulario”,ok:false,...}
你必须将快速api部署到你的服务器,这个链接可以帮助你https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/deployment
你正在使用开发模式在生产服务器和你的防火墙中工作阻塞端口3000,当你部署api时,你要将帖子发送到http://YOUR_IP/endpoint或http://api.IP/endpoint