This is my first time using cors and node.js, I am trying to call a Web API hosted on one URL from another URL.
I cannot even get cors to work simple app.use(cors());
I tried more complex configurations with origins and whatnot, still no luck.
The api lives on https://api.example.app and is being called from https://myapp.example.app
I am getting all kinds of cors errors.
Note, I am also using CloudFlare and Nginx Proxy Manager
Also, I CAN go to the https://api.example.app/select and get back my data that way without issue. But thats the only scenario working.
var express = require('express');
var mysql = require('mysql');
var cors = require('cors');
var app = express();
var port = 3333;
app.use(express.text());
app.use(cors());
var pool = mysql.createPool({
connectionLimit: 10,
host: '192.168.1.200',
port: 3306,
user: 'user',
password: 'password',
database: 'mydb'
});
app.get('/select', (req, res) => {
pool.query('SELECT * FROM mytable', (error, results, fields) => {
if (error) {
console.error('Error retrieving data:', error);
return res.status(500).send('Failed to retrieve data from database');
}
console.log('Data retrieved successfully:', results);
res.status(200).json(results);
});
});
app.post('/insert', (req, res) => {
var string = req.body;
console.log(req.body);
if (!string) {
return res.status(400).send('String is required');
}
pool.query('INSERT INTO mytable(user) VALUES (?)', string, (err, results) => {
if (err) {
console.error('Error inserting string into database:', err);
return res.status(500).send('Failed to insert string into database');
}
console.log('String inserted successfully');
return res.status(200).send('String inserted successfully');
});
});
app.listen(port, () => {
console.log(`Server is running on http://192.168.1.200:${port}`);
});
I think I have it back to working with a combination of Cloudflare configs, but unfortunately, I am not confident in which settings are helping or not helping. All I know is that it works. So I don't really want to post an answer unless I can just say this, but I do want to close the issue...
Cloudflare settings seem to take a little while to not be in affect from my testing where it would work one day after changing a setting but not the next. Maybe a caching thing.
If someone wants to look at my CF config on a discord screenshare to help determine what should be setup or not. I would still be open to that to help come to a clear conclusion. Otherwise, its dust in the wind.
Thanks everyone.