繁体   English   中英

生产中的 Angular 7 Form

[英]Angular 7 Form in production

我正在尝试运行一个表单,该表单使用本地主机中的 nodemailer 将数据发送到邮件:3000 它运行良好,但是当我的项目与社区一起加载到服务器上时,我无法使用此代码

项目根目录下的一个应用程序叫做 nodeMensajeria / src / node modules、app.js 和 configMensajeria.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() {

  }

}

它告诉我应用程序已执行

但是在生产中发送表单时,它显示了以下错误

无法加载资源: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,/form好的:假的,...}

看错误

您必须将 express api 部署到您的服务器中,此链接可以帮助您https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/deployment

您正在使用开发模式在生产服务器中工作,并且您的防火墙阻止了端口 3000,当您部署 api 时,您必须将帖子发送到http://YOUR_IP/endpointhttp://api.IP /端点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM