Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work with Koa v1.2.0 #4

Open
mrchief opened this issue May 2, 2016 · 5 comments
Open

Doesn't work with Koa v1.2.0 #4

mrchief opened this issue May 2, 2016 · 5 comments

Comments

@mrchief
Copy link

mrchief commented May 2, 2016

app.use(serveStatic('./html', {extensions: true}));

AssertionError: app.use() requires a generator function

@mrchief
Copy link
Author

mrchief commented May 2, 2016

Here is my solution:

  const originalServeStatic = serveStatic(...);

  app.use(function* (){
    yield new Promise((resolve, reject) => {
      // hacked statusCode
      if (this.status === 404) this.status = 200;
      // unnecessary response by koa
      this.respond = false;
      // 404, serve-static forward non-404 errors
      // force throw error
      originalServeStatic(this.req, this.res, reject);
    })
  });

@ghost
Copy link

ghost commented Aug 6, 2016

Working for anyone?

@fundon
Copy link
Member

fundon commented Aug 7, 2016

Which is version of serve-static in your project?

@ghost
Copy link

ghost commented Aug 7, 2016

@fundon - npm ls tells me I'm runnning the original serve-static 1.11.1.

This is my working code using Express' serve-static. However, the module won't be able to use yield with 'use strict' enabled. I'm not sure of a workaround you can use in your module.

// app.js

var serveStatic = require('serve-static');
var originalServeStatic = serveStatic(...);

app.use(function* (){
    try {
        yield new Promise((resolve, reject) => {
            this.status = 200;
            originalServeStatic(this.req, this.res, reject);
        });
    } catch (error) {
        this.status = 404;
    }
});

@fundon
Copy link
Member

fundon commented Aug 9, 2016

If want to work with koa v1, should install serve-static 0.x( < 1.0).
If want to work with koa v2, should install serve-static 1.x( >= 1.0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants