Failed to load resource: net::ERR_CONNECTION_REFUSED : Nodejs

38,002

Solution 1

Failed to load resource: net::ERR_CONNECTION_REFUSED : :3000/contact/send:1

Your error means your client application is Unable to connect to your nodejs server.

Fix

Your code :

const uri = 'http://localhost:3000/contact/send';

Will work if nodejs server is running on port 3000 on localhost.

Solution 2

For anyone else getting this error. Check that your NodeJS is not crashing. If a previous request causes your NodeJS server to crash or restart this is the same error on subsequent requests.

Share:
38,002

Related videos on Youtube

The Dead Man
Author by

The Dead Man

Updated on February 02, 2022

Comments

  • The Dead Man
    The Dead Man over 2 years

    I have a bootstrap form for email services for angular 6 app and nodejs, I am using nodemailer for sendemail in my app, unfortunatelly does not work. I ma getting the following error:

    Failed to load resource: net::ERR_CONNECTION_REFUSED : :3000/contact/send:1

    here is the form

     <form [formGroup]="angForm" novalidate>
        <div class="message">
          <h3> Write to us </h3>
        </div>
        <div class="form__top">
          <div class="form__left">
            <div class="form__group">
              <input class="form__input form__input--name" type="text"   formControlName="name" placeholder="name" #name>
            </div>
            <div *ngIf="angForm.controls['name'].invalid && (angForm.controls['name'].dirty || angForm.controls['name'].touched)" class="alert alert-danger">
              <div *ngIf="angForm.controls['name'].errors.required">
                Name is required.
              </div>
            </div>
            <div class="form__group">
              <input class="form__input form__input--email" type="email"  formControlName="email" placeholder="email" #email>
            </div>
            <div *ngIf="angForm.controls['email'].invalid && (angForm.controls['message'].dirty || angForm.controls['message'].touched)"
              class="alert alert-danger">
              <div *ngIf="angForm.controls['message'].errors.required">
                message is required.
              </div>
            </div>
          </div>
          <div class="form__right">
            <div class="form__group">
              <textarea class="form__input form__input--textarea" placeholder="Message" formControlName="message"  #message
                rows="3"></textarea>
            </div>
            <div *ngIf="angForm.controls['message'].invalid && (angForm.controls['message'].dirty || angForm.controls['message'].touched)"
              class="alert alert-danger">
              <div *ngIf="angForm.controls['message'].errors.required">
                message is required.
              </div>
            </div>
          </div>
        </div>
        <flash-messages></flash-messages>
        <div class="form__down">
          <div class="form__group">
            <button (click)="sendMail(name.value, email.value, message.value)" [disabled]="angForm.pristine || angForm.invalid"  class="form__input form__input--submit" name="submit" type="submit" value="SEND MESSAGE">SEND MESSAGE
            </button>
          </div>
        </div>
    
      </form>
    

    Here is contact,js (node mailer settings and routes)

    const express = require('express');
    const router = express.Router();
    const request = require('request');
    const nodemailer = require('nodemailer');
    
    router.get('/send', (req, res) => {
        const outputData = `
        <p>You have a new contact request</p>
        <h3>Contact Details</h3>
        <ul>  
          <li>Name: ${req.body.name}</li>
          <li>Email: ${req.body.email}</li>
        </ul>
        <h3>Message</h3>
        <p>${req.body.message}</p>
      `;
    
        let transporter = nodemailer.createTransport({
            service: 'gmail',
            secure: false,
            port: 25,
            auth: {
                user: 'MY EMAIL',
                pass: 'THE PASSWORD'
            },
            tls: {
                rejectUnauthorized: false
            }
        });
    
        let HelperOptions = {
            from: '"MYNAME" <MYEMAIL,
            to: 'MYEMAIL',
            subject: 'Majeni Contact Request',
            text: 'Hello',
            html: outputData
        };
    
    
    
        transporter.sendMail(HelperOptions, (error, info) => {
            if (error) {
                return console.log(error);
            }
            console.log("The message was sent!");
            console.log(info);
        });
    
    });
    module.exports = router;
    

    here is the server js.

    // server.js
    const express = require('express');
    const bodyParser = require('body-parser');
    const cors = require('cors');
    const path = require('path');
    const app = express();
    // Port Number
    const port = process.env.PORT || 3000
    // Run the app by serving the static files
    // in the dist directory
    app.use(express.static(path.join(__dirname, '/majeni/dist/majeni')));
    // Body Parser Middleware
    app.use(bodyParser.json());
    
    //routes
    const contact = require('./app/routes/contact');
    app.use('/contact', contact);
    // CORS Middleware
    app.use(cors());
    // If an incoming request uses
    // a protocol other than HTTPS,
    // redirect that request to the
    // same url but with HTTPS
    const forceSSL = function () {
      return function (req, res, next) {
        if (req.headers['x-forwarded-proto'] !== 'https') {
          return res.redirect(
            ['https://', req.get('Host'), req.url].join('')
          );
        }
        next();
      }
    }
    
    // Instruct the app
    // to use the forceSSL
    // middleware
    app.use(forceSSL());
    
    // For all GET requests, send back index.html
    // so that PathLocationStrategy can be used
    app.get('/*', function (req, res) {
      res.sendFile(path.join(__dirname + '/majeni/dist/majeni/index.html'));
    });
    
    // Start Server
    app.listen(port, () => {
        console.log('Server started on port '+port);
      });
    

    UPDATE

    Here is service

    import { Injectable } from '@angular/core';
    import { Headers, Http, Response } from '@angular/http';
    import { Jsonp } from '@angular/http';
    @Injectable({
      providedIn: 'root'
    })
    export class ContactService {
    
      constructor(private http: Http) { }
    
      sendEmail(name, email, message) {
        const uri = 'http://localhost:3000/contact/send';
        const obj = {
          name: name,
          email: email,
          message: message,
        };
        return this.http.post(uri, obj);
      }
    }
    

    What is missing in my code?

  • The Dead Man
    The Dead Man over 5 years
    Your right, but now I have the following error home:1 Failed to load http://localhost:3000/contact/send: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access what do I need to change in my codes?
  • msbit
    msbit over 5 years
    New issue, new question. Particularly if you’ve accept this as the answer to this question :)
  • The Dead Man
    The Dead Man over 5 years