Skip to content

Instantly share code, notes, and snippets.

@ayal
Created March 5, 2023 08:03
Show Gist options
  • Save ayal/9b577d0459b709594f8c387c735a676c to your computer and use it in GitHub Desktop.
Save ayal/9b577d0459b709594f8c387c735a676c to your computer and use it in GitHub Desktop.
simple node server with cors allow all
const http = require('http');
const hostname = '127.0.0.1';
const port = 3300;
const server = http.createServer((req, res) => {
console.log('got request', req.method);
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', '*');
res.setHeader('Access-Control-Allow-Headers', '*');
res.setHeader('Access-Control-Allow-Private-Network', 'true');
res.setHeader('Access-Control-Max-Age', '86400');
res.setHeader('Access-Control-Allow-Credentials', 'true');
if (req.method === 'OPTIONS') {
// Set CORS headers for preflight requests
res.statusCode = 200;
res.end();
}
else if (req.url === '/xxx' && req.method === 'GET') {
res.setHeader('Content-Type', 'application/json');
res.statusCode = 200;
res.end(JSON.stringify({ some:1, json: "123" }));
} else {
res.statusCode = 404;
res.end();
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment