NGINX Modern Apps > Class 3 - NGINX Dataplane Scripting Source | Edit on
File IO [misc/file_io]¶
With njs, NGINX is able to read and write files on disk. In this example, data is pushed to a file via HTTP POSTs. We can then read the file using HTTP GET as well as erase the file and start over.
Step 1: Use the following commands to start your NGINX container with this lab’s files:
Step 2: Now let’s use curl to test our NGINX server:
curl http://localhost/read
200 <empty reply>
curl http://localhost/push -X POST --data 'AAA'
200
curl http://localhost/push -X POST --data 'BBB'
200
curl http://localhost/push -X POST --data 'CCC'
200
curl http://localhost/read
200 AAABBBCCC
curl http://localhost/flush -X POST
200
curl http://localhost/read
200 <empty reply>
docker stop njs_example
Code Snippets¶
Notice how the push, flush, and read operations are triggered through NGINX location blocks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
This njs code uses a file on the NGINX server’s disk to provide persistent storage.