Skip to content

Instantly share code, notes, and snippets.

@zoonderkins
Created December 19, 2017 15:56
Show Gist options
  • Save zoonderkins/009495af060624bd82f2a846c0b81f6c to your computer and use it in GitHub Desktop.
Save zoonderkins/009495af060624bd82f2a846c0b81f6c to your computer and use it in GitHub Desktop.
[Enable CORS on Server] allow server to request CORS #javascript #node #cors
app.all('*', (req, res, next) => {
res.header("Access-Control-Allow-Origin", req.headers.origin || '*');
res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("Access-Control-Allow-Credentials", true); //可以带cookies
res.header("X-Powered-By", '3.2.1')
if (req.method == 'OPTIONS') {
res.send(200);
} else {
next();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment