Hello,
I have a 1-click LAMP Droplet (Ubuntu). I’ve installed Node.js on it and I’ve added an express.js application.
With PM2 I made it run successfully on port 3002, if I use sudo pm2 list I can see that is online. I’m trying to reach endpoints on this running express.js app from my React website (which is on the same server), but I get CORS Failed as I see on Network tab(browser inspector window).
My express.js app has cors:
const express = require('express');
var cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
And in React, I’m doing the request like this:
await fetch('https://localhost:3002/contacts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
}).then(response => response.json())
.then(data => {
console.log(data);
}).catch((error) => {
console.log(error);
});
Ubuntu version: 20.04.2 LTS (Focal Fossa) PM2 version: 5.1.2 Node.js version: 16.13.1
Is something that I’m missing?
Thanks alot.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello,
I believe that a possible explanation would be that you are using https://localhost:3002 rather than http://localhost:3002. Note the extra s.
But as your Node app is running on http you are seeing the cors error.
Let me know how it goes!
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.