start a bare NodeJS container
docker pull node:22-alpine docker run -it --rm --entrypoint sh -p 3000:3000 node:22-alpine
and proceed with some CORS-enabled app within – that’s from dev.to
cd ~/
npm init -y
npm install express cors
vi server.js
const express = require('express');
const cors = require('cors');
const app = express();
// Enable CORS for all routes
app.use(cors());
app.get('/data', (req, res) => {
res.json({ message: 'Hello, CORS!' });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
and run the code
node server.js
on the workstation
make sure the tcp port is available
nmap -p 3000 localhost
eventually check what JSON it spits out without further ado
curl -si localhost:3000/data; echo
notice the CORS ACAO header!
Access-Control-Allow-Origin: *
and proceed with a full-blown test – point to it with a sample html page
vi test-cors.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CORS Test</title>
</head>
<body>
<h1>Testing CORS</h1>
<button id="fetchData">Fetch Data</button>
<div id="result"></div>
<script>
document.getElementById('fetchData').addEventListener('click', () => {
fetch('http://localhost:3000/data')
.then(response => response.json())
.then(data => {
document.getElementById('result').textContent = data.message;
})
.catch(error => {
console.error('Error:', error);
});
});
</script>
</body>
</html>
and open that with a web browser
google-chrome test-cors.html
https://nodejs.org/en/download
https://dev.to/dipakahirav/mastering-cors-the-definitive-guide-with-practical-examples-eml