Skip to content

Commit 419b6dc

Browse files
authored
Merge pull request #5 from danielwinkler/feature/FS-options
Allow the request to provide options to the FS lib
2 parents 339db76 + 26a3af8 commit 419b6dc

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,5 @@ You can modify the `opts` variable as per your need under the `Tests` tab of the
6868
1. If you want all the data to be written to a single file then you can modify the value of mode to appendFile instead of writeFile (More functions here: [Node FS](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback))
6969

7070
2. If you want each response to be stored in a different file, then you can provide a `uniqueIdentifier` such as `Date.now()` or some environment variable as a counter, and it'll be used generate unique file names. You can also make the value of uniqueIdentifier as `true` and the server will internally append a unique number to every file name.
71+
72+
3. You can provide options to the FS lib, e.g. `options: { encoding: 'base64' }`.

script.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ app.get('/', (req, res) => res.send('Hello, I write data to file. Send them requ
2121

2222
app.post('/write', (req, res) => {
2323
let extension = req.body.fileExtension || defaultFileExtension,
24-
fsMode = req.body.mode || DEFAULT_MODE;
24+
fsMode = req.body.mode || DEFAULT_MODE,
2525
uniqueIdentifier = req.body.uniqueIdentifier ? typeof req.body.uniqueIdentifier === 'boolean' ? Date.now() : req.body.uniqueIdentifier : false,
2626
filename = `${req.body.requestName}${uniqueIdentifier || ''}`,
27-
filePath = `${path.join(folderPath, filename)}.${extension}`;
27+
filePath = `${path.join(folderPath, filename)}.${extension}`,
28+
options = req.body.options || undefined;
2829

29-
fs[fsMode](filePath, req.body.responseData, (err) => {
30+
fs[fsMode](filePath, req.body.responseData, options, (err) => {
3031
if (err) {
3132
console.log(err);
3233
res.send('Error');

0 commit comments

Comments
 (0)