npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nginx-json-errors

v1.0.1

Published

Generates files and config for nginx to return errors in json format

Downloads

9

Readme

Simple solution to have NGINX return errors in JSON format instead of HTML. Heavily inspired from https://nimblea.pe/monkey-business/2015/03/26/replying-with-json-errors-from-your-nginx-fronted-json-api/

This repo contains one json files per http error, with the following schema:

{
    "code": "HTTP RESPONSE CODE",
    "message": "ERROR PHRASE: ERROR DESCRIPTION",
    "link": "URI FOR THE ERROR SPEC"
}

Those files can be re-generated as follow (assuming you have nodejs installed):

npm install
npm run build

(npm install should be run once only.)

Usage

  • Copy the nginx-errors folder in a place accessible by NGINX
  • Adapt the below config snippet to your needs and copy it in the NGINX server directive.
    error_page 511 /nginx-errors/511.json;
    error_page 507 /nginx-errors/507.json;
    error_page 506 /nginx-errors/506.json;
    error_page 451 /nginx-errors/451.json;
    error_page 431 /nginx-errors/431.json;
    error_page 429 /nginx-errors/429.json;
    error_page 428 /nginx-errors/428.json;
    error_page 424 /nginx-errors/424.json;
    error_page 423 /nginx-errors/423.json;
    error_page 422 /nginx-errors/422.json;
    error_page 505 /nginx-errors/505.json;
    error_page 504 /nginx-errors/504.json;
    error_page 503 /nginx-errors/503.json;
    error_page 502 /nginx-errors/502.json;
    error_page 501 /nginx-errors/501.json;
    error_page 500 /nginx-errors/500.json;
    error_page 426 /nginx-errors/426.json;
    error_page 417 /nginx-errors/417.json;
    error_page 416 /nginx-errors/416.json;
    error_page 415 /nginx-errors/415.json;
    error_page 414 /nginx-errors/414.json;
    error_page 413 /nginx-errors/413.json;
    error_page 412 /nginx-errors/412.json;
    error_page 411 /nginx-errors/411.json;
    error_page 410 /nginx-errors/410.json;
    error_page 409 /nginx-errors/409.json;
    error_page 408 /nginx-errors/408.json;
    error_page 407 /nginx-errors/407.json;
    error_page 406 /nginx-errors/406.json;
    error_page 405 /nginx-errors/405.json;
    error_page 404 /nginx-errors/404.json;
    error_page 403 /nginx-errors/403.json;
    error_page 402 /nginx-errors/402.json;
    error_page 401 /nginx-errors/401.json;
    error_page 400 /nginx-errors/400.json;

    location ^~ /nginx-errors/ {
        internal;
        root   <REPLACE BY PATH TO A THE PARENT FOLDER OF THE COPIED NGINX-FOLDER>;
    }

Content-type

NGINX needs to return a content-type: application/json header for the above to work. A simple solution is to have it in the types list:

types {
...
    application/json                      json;
...
}

Alternatively, you can use:

        more_set_headers 'Content-Type: application/json charset=UTF-8';

In the location directive above, this requires to have the https://github.com/openresty/headers-more-nginx-module module installed.