简体   繁体   English

生产中的 Angular 7 Form

[英]Angular 7 Form in production

I am trying to put into operation a form that sends me data to the mail with nodemailer in localhost: 3000 it works well but when loading my project on the server with the community I can not get this code to work我正在尝试运行一个表单,该表单使用本地主机中的 nodemailer 将数据发送到邮件:3000 它运行良好,但是当我的项目与社区一起加载到服务器上时,我无法使用此代码

an application in the root of the project called nodeMensajeria / src / node modules, app.js and configMensaje.js项目根目录下的一个应用程序叫做 nodeMensajeria / src / node modules、app.js 和 configMensajeria.js

mira la raiz de mi proyecto app.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 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);
  });
}

the service message.service.ts服务 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);
  }
}

I am using the ip of my domain我正在使用我的域的 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它告诉我应用程序已执行

but when sending the form in production it shows me the following error但是在生产中发送表单时,它显示了以下错误

Failed to load resource: 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, …}无法加载资源: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好的:假的,...}

look at the error看错误

you have to deploy the express api into your server, this link could help you https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/deployment您必须将 express api 部署到您的服务器中,此链接可以帮助您https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/deployment

you are using the developement mode to work in the production server and your firewall its blocking the port 3000, when you deploy the api, you've to send the post to http://YOUR_IP/endpoint or http://api.IP/endpoint您正在使用开发模式在生产服务器中工作,并且您的防火墙阻止了端口 3000,当您部署 api 时,您必须将帖子发送到http://YOUR_IP/endpointhttp://api.IP /端点

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

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