ОШИБКА HttpErrorResponse, net :: ERR_CONNECTION_REFUSED - PullRequest
0 голосов
/ 24 сентября 2019

Я работаю над своим первым приложением на Angular.Я хочу отправлять электронные письма из контактной формы, но у меня есть ошибка, я не понимаю ее происхождение.

server.js

var nodemailer = require('nodemailer');
const http       = require("http");
const cors = require("cors");
const bodyParser = require("body-parser");
const express    = require("express");
const path       = require("path");
const api = require("./server/routes/api");
// create a new Express application instance 
const app = express();
console.log( "dans server.js");
app.use(cors({origin:"*" })) ;
app.use(express.static('public')); 
app.use(bodyParser.json());
console.log( "dans server.js");
//start application server on port 3000
server.listen(app.get('port'), function(){
console.info('Server listening on port ' + app.get('port'));
});
app.listen(3000, () => {
console.log("The server started on port 3000");
});
app.get("/",(req,res)=>{
res.send("");
});
app.post("/sendemail", (req, res) => {
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'emailsender@gmail.com',
pass: '*******'
}
});

var mailOptions = {
from: 'emailsender@gmail.com',
to: 'reciever@gmail.com',
subject: "hi with angular",
text: " hello great i sent u an email!"
};

transporter.sendMail(mailOptions, function(error, info) {
if (error) console.log(error);
else console.log("Message sent successfully: " + info.response);
});
});

содержимое файла rout.js: router.post('/sendmail', actions.sendmail);

код отправки почты в

contactform.ts:

mysubmit(fo)
{console.log("submit");

this._MessageService.sendMessage (this.user) .subscribe (() => { 
console.log(" service called submit");
console.log ("Contact form", "Message sent successfully", "success");
 }}

messageservice.ts

import {Injectable} from '@angular/core'; 
import { HttpClient } from '@angular/common/http';
@Injectable () 
export class MessageService { 
constructor (private _http: HttpClient) {} 
sendMessage (body) { 
return this._http.post ('http://localhost:3000/sendmail', body); 
}}

package.json:
{
"name": "perfect",
"version": "0.0.0",
"main": "Server.js",
etc....
}

Сообщение об ошибке:

HttpErrorResponse {заголовки: HttpHeaders, статус: 0, statusText: «Неизвестно
Ошибка», URL: ноль, ок: ложь,…} ОПЦИИ http://localhost:3000/sendmail net :: ERR_CONNECTION_REFUSED

может быть, картина объясняет больше ошибки

заранее спасибо !!

...