0

I don't know what's wrong with my code. I created a form with angular 8, send it node server, it's working and all is cool. Now I'm going live on the server. The request is going net::ERR_CONNECTION_TIMED_OUT I tried different options online and nothing works always give the same error.

  this.http.post('https://nirrob.com:8081/sendmail', formData).subscribe(
      (response) => console.log(response),
      (error) => console.log(error)
 )

node js server code //

const path = require('path');
const express = require('express');
const multer = require('multer');
const fs = require('fs');
const bodyParser = require('body-parser')
const app = express();
const nodemailer = require("nodemailer");
const DIR = './uploads';

    let storage = multer.diskStorage({
        destination: (req, file, cb) => {
          cb(null, DIR);
        },
        filename: (req, file, cb) => {
          cb(null, file.fieldname + '-' + Date.now() + '.' + path.extname(file.originalname));
        }
    });
    let upload = multer({storage: storage});

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: true}));

    app.use(function (req, res, next) {
      res.setHeader('Access-Control-Allow-Origin', '*');
      res.setHeader('Access-Control-Allow-Methods', 'POST');
      res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
      res.setHeader('Access-Control-Allow-Credentials', true);
      next();
    });

    var attachedFiles = upload.fields([{ name: 'registration', maxCount: 1 }, { name: 'permit', maxCount: 1 }, { name: 'commerce', maxCount: 1 }, { name: 'companyFile', maxCount: 1 }, { name: 'serviceFile', maxCount: 1 }, ])

    app.post('/sendmail', attachedFiles, function (req, res) {
        if (req.files) {
            let data = req.body;
            console.log(req.files.serviceFile[0]);
            async function main() {
      // Generate test SMTP service account from ethereal.email
      // Only needed if you don't have a real mail account for testing


      // create reusable transporter object using the default SMTP transport
      let transporter = nodemailer.createTransport({
              ////// info
      });



      // send mail with defined transport object
     transporter.sendMail({
            ///info

              });


    }

          main().catch(console.error);
            return res.send({
              success: false
            });

          } else {
            console.log('file received');
            return res.send({
              success: true
            })
          }
    });

    const PORT = process.env.PORT || 8081;

    app.listen(PORT, function () {
      console.log('Node.js server is running on port ' + PORT);
    });
John
  • 10,165
  • 5
  • 55
  • 71
  • Can you confirm that the request is being received by the server? Are all cases handled correctly? I mean, do you resolve the request even though it may fail on the server? (I have not studied your code) – John Dec 24 '19 at 09:45
  • I'm not sure how to confirm if it was received by the server, it's a timeout as far as I know, angular received no reply from the server so I assume it was never received. https://nirrob.com:3001/sendmail net::ERR_CONNECTION_TIMED_OUT also, I'm opening the terminal on port 3001 on server, listening to it but absolutely nothing – ibrahim Mohamed Dec 24 '19 at 10:02
  • You can resolve/return the request immediately in the method to confirm that the server is receiving the request. If that is not the case, then you have some routing issues. If the server receives the request, you need to figure out why the server times out. It could be a condition that is not met, so the server never resolves the request. I am not sure, but you could try those options at least. – John Dec 24 '19 at 10:05
  • thank you, I found out that it's due to CORS error: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://nirrob.com:3001/sendmail. (Reason: CORS request did not succeed) – ibrahim Mohamed Dec 24 '19 at 10:24
  • I'm allowing CORS in my .htaccess and on the server.js in node backend, I don't know what to do – ibrahim Mohamed Dec 24 '19 at 10:25
  • Maybe this question can help: https://stackoverflow.com/questions/18310394/no-access-control-allow-origin-node-apache-port-issue, but I agree. It looks like the headers are set correctly. Try to switch the wild card with the actual origin. Instead of `*` use `https://your.domain.com`. – John Dec 24 '19 at 10:29
  • I tried that this morning, it didn't work, the issue is, it's working locally! Is it my hosting? I'm going mad to find out the reason – ibrahim Mohamed Dec 24 '19 at 10:36

1 Answers1

0

After long hours, it was the hosting. They were limiting which ports to use. So if you faced the same thing as me, it works locally but not on the server. Be smart and contact your hosting. I wasn't ^^"