簡體   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