Search code examples
apacheapinode.jsgziphttp-caching

JSON-API in NodeJS behind Apache webserver: Best practices to handle caching and compression?


I wrote a JSON-API in NodeJS for a small project, running behind an Apache webserver. Now I'd like to improve performance by adding caching and compression. Basically, the question is what should be done in NodeJS itself and what is better handled by Apache:

a) The API calls have unique URLs (e.g. /api/user-id/content) and I want to cache them for at least 60 seconds.

b) I want the output to be served as Gzip (if it's understood by the client). NodeJS's HTTP module usually delivers content as "chunked". As I'm only writing a response in one place, is it enough to adjust the Content-encoding header to serve it as one piece so it can be compressed and cached?


Solution

  • Ok, as my API has only a very very basic use, I'll go with an little in-memory key/value store as basic cache (based on the inspiration Simple Cache gave me). For this little development experiment, that should be enough. For an API in production use, I'd stick to Alfred's tipps.

    For the compression I'll use Apache's mod_deflate. It's robust and I don't need async gzipping at this point. Furthermore you can change compression settings without changing the app itself.

    Thank you both for your help!