question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

server crashes when cors is added

See original GitHub issue

Describe the bug Passing const cors as an argument to server and sending a postmethod to the defined endpoint, throws internal server error and, middleware error on the console

To Reproduce Steps to reproduce the behavior:

  1. npm i server
  2. touch index.js
  3. 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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
franciscopcommented, Jan 25, 2019

I didn’t notice that in my first read, but that seems to be the case. There are two issues though:

  • The error is not clear enough. I think adding the path and method would be much more clear, like The POST /demo request did not return anything.
  • That example is right in the documentation. All documentation examples should be working
1reaction
SidneyNemzercommented, Jan 24, 2019

That error is unrelated to CORS, it’s complaining that the /demo route didn’t return any data to the client

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found