lukeed / polka

A micro web server so fast, it'll make you dance! :dancers:
MIT License
5.39k stars 172 forks source link

Build a list of native or Polka-compatible middleware #85

Open guilhermeoc97 opened 5 years ago

guilhermeoc97 commented 5 years ago

Pretty much what it says on the title. It would be awesome to know which middlewares are compatible with Polka or made exclusively with Polka in mind.

So far I haven't found any, but maybe some middleware can break while running on Polka because it uses IncomingMessage and ServerResponse while Express uses their own implementation of a request module and a response module.

If deemed unnecessary, the issue can be closed. But for now, here are the lists:

Native to Polka

Compatible, tested by the creator

Compatible, tested by users

Incompatible

neves commented 5 years ago

I can share the ones that I'm using:

  1. response-time
  2. cors
  3. morgan
  4. favicon
  5. body-parser
GrosSacASac commented 5 years ago

serve-static https://github.com/lukeed/polka/blob/master/examples/with-serve-static/index.js

GrosSacASac commented 5 years ago

Recently made onewaydata compatible with polka

fraxken commented 4 years ago

multer seem to bug with polka (unhandled promises when the fileSize limit is reached). I use make-promises-safe so my server as been shutted down. I guess there is a problem around how the error is handled in the inner multer middleware.

lukeed commented 4 years ago

@fraxken what version of polka? can you provide a small repo?

fraxken commented 4 years ago

@lukeed https://github.com/fraxken/polka_multer

At first i failed to reproduce the problem (so i get back to my project to reproduce on it and found what could produce a difference).

The bug only appear if i create a new polka instance at create the route on it

const app = polka({ onError });

const router = polka();
router.post("/avatar", upload.single("avatar"), async (req, res) => {

});
app.use("/api", router);

Make no sense to do this in the reproduction sure (but in my app i require many "sub-routes" to use them in the /api namespace).

It produce the following error

_http_server.js:237
    throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
    ^

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: LIMIT_FILE_SIZE
    at ServerResponse.writeHead (_http_server.js:237:11)
    at ServerResponse._implicitHeader (_http_server.js:228:8)
    at write_ (_http_outgoing.js:616:9)
    at ServerResponse.end (_http_outgoing.js:733:5)
    at Polka.onError (F:\Code\synergized\node-server\node_modules\polka\index.js:22:6)
    at next (F:\Code\synergized\node-server\node_modules\polka\index.js:94:32)
    at Immediate._onImmediate (F:\Code\synergized\node-server\node_modules\multer\lib\make-middleware.js:53:37)
    at processImmediate (internal/timers.js:441:21) {
  code: 'ERR_HTTP_INVALID_STATUS_CODE'
}

Maybe i missed something on how to use this properly.. not sure.

Best Regards, Thomas

ohmdob commented 4 years ago

Work for me to avoid Server crashed

    .post('/uploadfile', function (req, res, next) {
        const uploadCb = upload.single('file')
        uploadCb(req, res, function (err) {
            if (err instanceof multer.MulterError) {
                res.writeHead(500);
                res.end('A Multer error occurred when uploading.');
            } else if (err) {
                res.writeHead(500);
                res.end('An unknown error occurred when uploading.');
            }
            res.writeHead(200, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify(req.file));
        })
    })

More information https://github.com/expressjs/multer [Error Handing]

floratmin commented 2 years ago

I build a lightweight and extendable body-parser like middleware: modular-body