javascriptnode.jsnode-static

node-static add headers via cli


I have a question about cli use of the node-static library.

I can go to a folder and just run static. Which serves the directory on port 8080.

I can see that -H is the option to format but I'm not sure how to pass in the options. Here is what I'm trying:

static -H "{'Access-Control-Allow-Origin': '*'}"

Also:

static -H {'Access-Control-Allow-Origin': '*'}

Which returns undefined:1

What is the proper way to pass the header option?


Solution

  • In the node-static README it shows the -H option taking a JSON value:

    # specify additional headers (this one is useful for development) 
    $ static -H '{"Cache-Control": "no-cache, must-revalidate"}'
    serving "." at http://127.0.0.1:8080
    

    ...and we can see in the source that it uses JSON.parse.

    Neither of your attempts is valid JSON. In JSON, property names and strings must be surrounded by double quotes:

    $ static -H '{"Access-Control-Allow-Origin": "*"}'