franciscop / server

:desktop_computer: Simple and powerful server for Node.js
https://serverjs.io/
MIT License
3.56k stars 171 forks source link

When trying to render page I get ERROR { Error: Your middleware did not return anything to the user. This normally happens when no route was matched or if the router did not reply with anything. #118

Closed AdamCollins closed 5 years ago

AdamCollins commented 5 years ago

I am trying to render a simple EJS page(currently doesn't contain any variables) when a user goes to the route directory. The page does render but I get the warning in my title.

const server = require('server');
const { get, error } = server.router;
const { render, status} = server.reply;

server([
    get('/',ctx => render('index.ejs'))
]);

From reading this issuehere I thought I may be missing some sort of error handling so I tried adding

error(ctx => status(500).send(ctx.error.message))

to my server function but with no prevail.

Also, I noticed it does work but if I change my the server function to this

server(ctx => render('index.ejs'));

but obviously I would like to have more than one route.

Any help would be appreciated!

Environment (optional):

franciscop commented 5 years ago

Thanks for the complete bug report, I'll try to reproduce later. This does seems like a server.js bug.

franciscop commented 5 years ago

Hi @AdamCollins, unfortunately I couldn't reproduce the issue and it works fine for me. Could you please put your package.json and copy/paste the full error? I suspect one of these 3 issues might be happening:

const server = require("server");
const { get, error } = server.router;
const { render, status } = server.reply;

server([
  ctx => {
    console.log(ctx.method, ctx.url);
  },
  get("/", ctx => render("index.ejs"))
]);

There might be some other issue, but these 3 are the ones I'd check for first. I created a repository with working code. Is there anything different from your code?

franciscop commented 5 years ago

BTW I strongly believe it is the 3rd option of the browser asking for a resource that cannot be found, because it's the only one that would account for the situation you are seeing that server(get('/', ctx => ...)) does not work but server(ctx => ...) works.

AdamCollins commented 5 years ago

The issue was option 3. After adding the references to the public folder the warning disappears. Thank you!

franciscop commented 5 years ago

Hey @AdamCollins, so I also went ahead and just published 1.0.19 fixing the issue on server.js side. Now the error message will be much more clear like this:

Your middleware did not return anything for this request:

GET /abc.png

This normally happens when no route was matched or if the router did not reply with anything. Make sure to return something, even if it's a catch-all error.

Documentation for reply: https://serverjs.io/documentation/reply/
Relevant issue: https://github.com/franciscop/server/issues/118

Please feel free to open any other issue you find 😄