server crashes when cors is added
See original GitHub issueDescribe the bug
Passing const cors
as an argument to server
and sending a post
method to the defined endpoint, throws internal server error
and, middleware
error on the console
To Reproduce Steps to reproduce the behavior:
npm i server
touch index.js
- Add this
// Include it and extract some methods for convenience
const server = require('server');
const { get, post } = server.router;
const cors = [
ctx => header("Access-Control-Allow-Origin", "*"),
ctx => header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"),
ctx => ctx.method.toLowerCase() === 'options' ? 200 : false
];
// Launch server with options and a couple of routes
server({ port: 8080 }, cors, [
get('/', ctx => 'Hello world'),
post('/demo', ctx => console.log(ctx.data))
]);
4 node index.js
5. Send a post request to: http://localhost:8080/demo
Expected behavior Some data to returns. Instead getting errors.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Crash CORS: Cookies, Servers and Webpages - Medium
When I ran a request from localhost:3000 to access data on another webpage, my request failed because Github Jobs did not explicitly give...
Read more >Crash CORS: A Guide for Using CORS - Backblaze
Adding CORS rules to your bucket tells Backblaze B2 which preflight requests to approve. You can enable CORS in the Backblaze B2 UI...
Read more >API retrurning 500 and CORS error without even starting the ...
The registration controller fails with 500 internal server error and instant CORS error saying my origin can't access the resource(I'm using ...
Read more >Crash Course in CORS - JavaScript in Plain English
CORS configuration is mainly handled server-side. We basically have to set some headers that will be sent with the response from the server....
Read more >Understanding Cross Origin Resource Sharing (CORS)
Cross-origin resource sharing (CORS) is a mechanism that allows a client application to request restricted resources hosted on server from a ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I didn’t notice that in my first read, but that seems to be the case. There are two issues though:
The POST /demo request did not return anything
.That error is unrelated to CORS, it’s complaining that the /demo route didn’t return any data to the client