-
Notifications
You must be signed in to change notification settings - Fork 105
/
script.js
44 lines (37 loc) · 1.56 KB
/
script.js
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const express = require('express'),
app = express(),
fs = require('fs'),
shell = require('shelljs'),
// Modify the folder path in which responses need to be stored
folderPath = './Responses/',
defaultFileExtension = 'json', // Change the default file extension
bodyParser = require('body-parser'),
DEFAULT_MODE = 'writeFile',
path = require('path');
// Create the folder path in case it doesn't exist
shell.mkdir('-p', folderPath);
// Change the limits according to your response size
app.use(bodyParser.json({limit: '50mb', extended: true}));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.get('/', (req, res) => res.send('Hello, I write data to file. Send them requests!'));
app.post('/write', (req, res) => {
let extension = req.body.fileExtension || defaultFileExtension,
fsMode = req.body.mode || DEFAULT_MODE,
uniqueIdentifier = req.body.uniqueIdentifier ? typeof req.body.uniqueIdentifier === 'boolean' ? Date.now() : req.body.uniqueIdentifier : false,
filename = `${req.body.requestName}${uniqueIdentifier || ''}`,
filePath = `${path.join(folderPath, filename)}.${extension}`,
options = req.body.options || undefined;
fs[fsMode](filePath, req.body.responseData, options, (err) => {
if (err) {
console.log(err);
res.send('Error');
}
else {
res.send('Success');
}
});
});
app.listen(3000, () => {
console.log('ResponsesToFile App is listening now! Send them requests my way!');
console.log(`Data is being stored at location: ${path.join(process.cwd(), folderPath)}`);
});