Home
Support Forums

My deploy in prod got cors issue or even work at all

Hi,

I am having more than one problem in deploy-production.

I have a client made with react on netlify and an express.js server as backend on heroku.

Using netlify dev everything works fine but when I deploy in prod I have different issues.

The first one was that I put the heroku link address on top of every fetch request from the client to the backend to make it work.

Unfortunately, the one that still does not work is the one I am sending the request to nodemailer .
Every time I got :

Blockquote
Access to fetch at ‘https://lyrics-bites-dev.herokuapp.com/api/send_email/I%20bless%20the%20day%20I%20found%20youI%20want%20my%20arms%20around%20youAnd%20so%20I%20beg%20youLet%20it%20be%20meI%20don’t%20take%20this%20heaven%20from%20oneIf%20you%20must%20cling%20someoneNow%20and%20forever%20This%20Lyrics%20is%20NOT%20for%20Commercial%20use%20(1409619119014)’ from origin ‘https://616a048c3b396f1f7de16acc--silly-feynman-ab4c81.netlify.app’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.

Blockquote
VM169:1 GET https://lyrics-bites-dev.herokuapp.com/api/send_email/I%20bless%20the%20day%20I%20found%20youI%20want%20my%20arms%20around%20youAnd%20so%20I%20beg%20youLet%20it%20be%20meI%20don’t%20take%20this%20heaven%20from%20oneIf%20you%20must%20cling%20someoneNow%20and%20forever%20This%20Lyrics%20is%20NOT%20for%20Commercial%20use%20(1409619119014) net::ERR_FAILED 503

after that the log file on my heroku app are this:

Blockquote
2021-10-15T23:32:05.599214+00:00 heroku[web.1]: Starting process with command npm start
2021-10-15T23:32:06.571600+00:00 app[web.1]:
2021-10-15T23:32:06.571614+00:00 app[web.1]: > lyrics-bites-backend@1.0.0 start /app
2021-10-15T23:32:06.571614+00:00 app[web.1]: > node server.js
2021-10-15T23:32:06.571615+00:00 app[web.1]:
2021-10-15T23:32:06.919207+00:00 app[web.1]: db connected
2021-10-15T23:32:06.921505+00:00 app[web.1]: server running on 53698
2021-10-15T23:32:07.288195+00:00 heroku[web.1]: State changed from starting to up
2021-10-15T23:32:15.723465+00:00 heroku[router]: at=info method=GET path="/api/all" host=lyrics-bites-dev.herokuapp.com request_id=7c397bf8-6b2a-45e5-ad48-214907d5ee52 fwd=“137.220.81.5” dyno=web.1 connect=0ms service=22ms status=200 bytes=2229 protocol=https
2021-10-15T23:32:20.445949+00:00 heroku[router]: at=error code=H13 desc=“Connection closed without response” method=GET path="/api/send_email/I%20say%20hey,%20MonaOh,%20MonaI%20say%20yeah,%20yeah,%20yeah,%20yeah,%20MonaOh,%20MonaI%20tell%20you%20Mona%20what%20I%20wanna%20doI’ll%20build%20a%20house%20next%20door%20to%20youCan%20I%20see%20you%20sometimes?We%20can%20go%20kissing%20through%20the%20blindsYeah,%20can%20I%20out%20come%20out%20on%20the%20frontAnd%20listen%20to%20my%20heart%20go%20bumpety%20bumpNeed%20you%20baby,%20that%27s%20no%20lie…%20This%20Lyrics%20is%20NOT%20for%20Commercial%20use%20(1409618696701)" host=lyrics-bites-dev.herokuapp.com request_id=e6d93756-e9b9-4b82-b4e9-58925bf8efbc fwd=“137.220.81.5” dyno=web.1 connect=0ms service=96ms status=503 bytes=0 protocol=https
2021-10-15T23:32:20.585588+00:00 heroku[web.1]: Process exited with status 1
2021-10-15T23:32:20.647756+00:00 heroku[web.1]: State changed from up to crashed
2021-10-15T23:32:20.352142+00:00 app[web.1]: I say hey, MonaOh, MonaI say yeah, yeah, yeah, yeah, MonaOh, MonaI tell you Mona what I wanna doI’ll build a house next door to youCan I see you sometimes
2021-10-15T23:32:20.441759+00:00 app[web.1]: /app/server.js:85
2021-10-15T23:32:20.441763+00:00 app[web.1]: throw error;
2021-10-15T23:32:20.441764+00:00 app[web.1]: ^
2021-10-15T23:32:20.441764+00:00 app[web.1]:
2021-10-15T23:32:20.441765+00:00 app[web.1]: Error: Missing credentials for “PLAIN”
2021-10-15T23:32:20.441766+00:00 app[web.1]: at SMTPConnection._formatError (/app/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
2021-10-15T23:32:20.441766+00:00 app[web.1]: at SMTPConnection.login (/app/node_modules/nodemailer/lib/smtp-connection/index.js:438:38)
2021-10-15T23:32:20.441767+00:00 app[web.1]: at /app/node_modules/nodemailer/lib/smtp-transport/index.js:271:32
2021-10-15T23:32:20.441767+00:00 app[web.1]: at SMTPConnection. (/app/node_modules/nodemailer/lib/smtp-connection/index.js:209:17)
2021-10-15T23:32:20.441768+00:00 app[web.1]: at Object.onceWrapper (events.js:519:28)
2021-10-15T23:32:20.441768+00:00 app[web.1]: at SMTPConnection.emit (events.js:400:28)
2021-10-15T23:32:20.441769+00:00 app[web.1]: at SMTPConnection._actionEHLO (/app/node_modules/nodemailer/lib/smtp-connection/index.js:1319:14)
2021-10-15T23:32:20.441769+00:00 app[web.1]: at SMTPConnection._processResponse (/app/node_modules/nodemailer/lib/smtp-connection/index.js:947:20)
2021-10-15T23:32:20.441769+00:00 app[web.1]: at SMTPConnection._onData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:749:14)
2021-10-15T23:32:20.441785+00:00 app[web.1]: at TLSSocket.SMTPConnection._onSocketData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:189:44) {
2021-10-15T23:32:20.441786+00:00 app[web.1]: code: ‘EAUTH’,
2021-10-15T23:32:20.441786+00:00 app[web.1]: command: ‘API’
2021-10-15T23:32:20.441786+00:00 app[web.1]: }
2021-10-15T23:32:20.452863+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-10-15T23:32:20.453101+00:00 app[web.1]: npm ERR! errno 1
2021-10-15T23:32:20.456933+00:00 app[web.1]: npm ERR! lyrics-bites-backend@1.0.0 start: node server.js
2021-10-15T23:32:20.456972+00:00 app[web.1]: npm ERR! Exit status 1
2021-10-15T23:32:20.457032+00:00 app[web.1]: npm ERR!
2021-10-15T23:32:20.457071+00:00 app[web.1]: npm ERR! Failed at the lyrics-bites-backend@1.0.0 start script.
2021-10-15T23:32:20.457110+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-10-15T23:32:20.462099+00:00 app[web.1]:
2021-10-15T23:32:20.462171+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-10-15T23:32:20.462198+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-10-15T23_32_20_457Z-debug.log

I am struggling for two days and it is the first time I am trying to deploy a mern application so bear with me if I added to may details.

My toml file is this:

Blockquote
[build]
functions = “netlify/functions/node-fetch”

[[redirects]]
from = “/lyrics/*”
to = “https://api.musixmatch.com/:splat
status = 200

[[redirects]]
from = “/cover/*”
to = “http://ws.audioscrobbler.com/:splat
status = 200

[[redirects]]
from = “/lyrcs/”
to = “/.netlify/functions/:splat”
status = 200

[[redirects]]
from = “/cover/”
to = “/.netlify/functions/:splat”
status = 200

[[redirects]]
from = “/api”
to = “https://lyrics-bites-dev.herokuapp.com/
status = 200
force = true
headers = {X-From = “Netlify”}

this is the request from the server I tried with asycn/await and without but nothing.

const sendLyrics = async () => {
    try {
      await fetch(`https://lyrics-bites-dev.herokuapp.com/api/send_email/${lyrics}`)
        .then((res) => res.json())
        .then((data) => console.log(data));
    } catch (error) {
      console.log(error);
    }
  };

and this is the route in express.js

app.get("/api/send_email/:lyrcs", async (req, res) => {
  const { lyrcs } = req.params;
  console.log(lyrcs);
  const transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    service: process.env.HOST,
    secure: true,
    auth: {
      user: process.env.USER,
      pass: process.env.PASSWORD,
    },
  });

  let messageOptions = {
    from: process.env.MAIL_FROM,
    to: process.env.USER2,
    subject: "schedule email",
    html: `<div
    style="
    font-family:monospace;
    margin: 0 auto;
    max-width: 400px;
    text-align: center;
    line-height: 2">
    ${lyrcs}
    </div>`,
  };

  await transporter.sendMail(messageOptions, (error, info) => {
    if (error) {
      throw error;
    } else {
      console.log("Email successfully sent!!!");
    }
  });
});

I am really confused now!! How can I solve the cors issue with nodemailer?

Thanks in advance
Banjer71

I’m a little confused. You already have this redirect rule as per what you’ve said.

Then I suppose that’s the code you’ve deployed on Netlify.

In that case, I can see 2 possible problems. Firstly, the redirect could be like this:

[[redirects]]
  from = “/api/*”
  to = “https://lyrics-bites-dev.herokuapp.com/:splat”
  status = 200
  force = true

And in the frontend you can do something like:

await fetch(`/api/send_email/${lyrics}`)

This would forward the request to Heroku without any CORS errors.

Hi,
thank you very much I think I sorted as you suggested and now I am not having any more cors issues, the only problem now is this:

2021-10-17T09:25:05.724203+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/api/send_email/Ladies%20and%20gentlemen,%20this%20is%20Tom%20JonesWithout%20a%20warning,%20you%20broke%20my%20heartYou%20took%20it,%20darling,%20and%20you%20tore%20it%20apartYou%20left%20me%20standing%20in%20the%20dark,%20cryin'Said%20your%20love%20for%20me%20was%20dyin'Turn%20it%20up,%20baby,%20turn%20it%20up,%20pleaseTurn%20on%20your%20light,%20let%20it%20shine%20on%20meLet%20it%20shine,%20shine,%20shine,%20let%20it%20shineOh!...*******%20This%20Lyrics%20is%20NOT%20for%20Commercial%20use%20*******(1409619119014)" host=lyrics-bites-dev.herokuapp.com request_id=74523901-54c7-40e8-9d31-4ddb01c97bf7 fwd="137.220.81.5, 100.64.0.207, 127.0.0.1,138.197.188.142" dyno=web.1 connect=0ms service=159ms status=503 bytes=0 protocol=https
2021-10-17T09:25:05.865107+00:00 heroku[web.1]: Process exited with status 1
2021-10-17T09:25:05.944870+00:00 heroku[web.1]: State changed from up to crashed
2021-10-17T09:25:05.566174+00:00 app[web.1]: Ladies and gentlemen, this is Tom JonesWithout a warning, you broke my heartYou took it, darling, and you tore it apartYou left me standing in the dark, cryin'Said your love for me was dyin'Turn it up, baby, turn it up, pleaseTurn on your light, let it shine on meLet it shine, shine, shine, let it shineOh!...******* This Lyrics is NOT for Commercial use *******(1409619119014)
2021-10-17T09:25:05.719757+00:00 app[web.1]: /app/server.js:84
2021-10-17T09:25:05.719758+00:00 app[web.1]:       throw error;
2021-10-17T09:25:05.719759+00:00 app[web.1]:       ^
2021-10-17T09:25:05.719759+00:00 app[web.1]: 
2021-10-17T09:25:05.719760+00:00 app[web.1]: Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
2021-10-17T09:25:05.719761+00:00 app[web.1]: 535 5.7.8  https://support.google.com/mail/?p=BadCredentials i17sm10796916wru.18 - gsmtp
2021-10-17T09:25:05.719762+00:00 app[web.1]:     at SMTPConnection._formatError (/app/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
2021-10-17T09:25:05.719762+00:00 app[web.1]:     at SMTPConnection._actionAUTHComplete (/app/node_modules/nodemailer/lib/smtp-connection/index.js:1536:34)
2021-10-17T09:25:05.719762+00:00 app[web.1]:     at SMTPConnection.<anonymous> (/app/node_modules/nodemailer/lib/smtp-connection/index.js:540:26)
2021-10-17T09:25:05.719763+00:00 app[web.1]:     at SMTPConnection._processResponse (/app/node_modules/nodemailer/lib/smtp-connection/index.js:947:20)
2021-10-17T09:25:05.719763+00:00 app[web.1]:     at SMTPConnection._onData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:749:14)
2021-10-17T09:25:05.719764+00:00 app[web.1]:     at TLSSocket.SMTPConnection._onSocketData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:189:44)
2021-10-17T09:25:05.719764+00:00 app[web.1]:     at TLSSocket.emit (events.js:400:28)
2021-10-17T09:25:05.719765+00:00 app[web.1]:     at addChunk (internal/streams/readable.js:293:12)
2021-10-17T09:25:05.719765+00:00 app[web.1]:     at readableAddChunk (internal/streams/readable.js:267:9)
2021-10-17T09:25:05.719766+00:00 app[web.1]:     at TLSSocket.Readable.push (internal/streams/readable.js:206:10) {
2021-10-17T09:25:05.719766+00:00 app[web.1]:   code: 'EAUTH',
2021-10-17T09:25:05.719766+00:00 app[web.1]:   response: '535-5.7.8 Username and Password not accepted. Learn more at\n' +
2021-10-17T09:25:05.719767+00:00 app[web.1]:     '535 5.7.8  https://support.google.com/mail/?p=BadCredentials i17sm10796916wru.18 - gsmtp',
2021-10-17T09:25:05.719767+00:00 app[web.1]:   responseCode: 535,
2021-10-17T09:25:05.719767+00:00 app[web.1]:   command: 'AUTH PLAIN'
2021-10-17T09:25:05.719767+00:00 app[web.1]: }
2021-10-17T09:25:05.728148+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-10-17T09:25:05.728324+00:00 app[web.1]: npm ERR! errno 1
2021-10-17T09:25:05.731069+00:00 app[web.1]: npm ERR! lyrics-bites-backend@1.0.0 start: `node server.js`
2021-10-17T09:25:05.731119+00:00 app[web.1]: npm ERR! Exit status 1
2021-10-17T09:25:05.731179+00:00 app[web.1]: npm ERR! 
2021-10-17T09:25:05.731224+00:00 app[web.1]: npm ERR! Failed at the lyrics-bites-backend@1.0.0 start script.
2021-10-17T09:25:05.731267+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-10-17T09:25:05.738235+00:00 app[web.1]: 
2021-10-17T09:25:05.738311+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-10-17T09:25:05.738344+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-10-17T09_25_05_731Z-debug.log

So I think it is only a problem with Gmail, which is not accepting my credentials. I allowed the low-secured app permission but still got that error… and I also checked and compared the password stored in the env file and on the Heroku env variable and they match…maybe I will try with another service but so far at least I am not having any cors issue anymore.

Thanks a lot!!!