added /file GET request for reading files stored locally

This commit is contained in:
Duncan Phelps 2021-04-28 16:48:35 -05:00
parent 54f9250e53
commit ace3670339
3 changed files with 23 additions and 5 deletions

16
api/file/index.js Normal file

@ -0,0 +1,16 @@
const URL = require('url');
const do_wb = require('../../src/util');
fs = require('fs');
module.exports = function (req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
const url = URL.parse(req.url, true);
if (!url.query.filename) return res.status(400).send("Must specify filename");
if (url.query.filename){
fs.readFile(url.query.filename, (err, body) => {
if (err) return res.status(500).send(err.message || err);
do_wb(req, body, url, res);
});
}
};

@ -10,16 +10,16 @@ module.exports = function (req, res) {
/* parse form data */
const form = formidable({ multiples: true, maxFileSize: 2 * 1024 * 1024 });
/*create temp file*/
const newFile = () => {
const tmpobj = tmp.fileSync();
console.log('File: ', tmpobj.name);
console.log('Filedescriptor: ', tmpobj.fd);
const tmpobj = tmp.fileSync({postfix: '.xlsx'});
return tmpobj.name;
}
const writeFile = (fileName, file) => {
// console.log(file);
fs.writeFileSync(fileName + '.xlsx', file, (err) => {
fs.writeFile(fileName, file, (err) => {
if (err) return res.status(500).send(err.message || err);
res.status(201).send(fileName);
});
}
form.parse(req, (err, fields, files) => {

@ -3,6 +3,7 @@ const app = express();
const data = require('./data/index');
const upload = require('./upload/index');
const save = require('./save/index');
const file = require('./file/index');
const bodyParser = require('body-parser');
const port = 3000;
@ -14,6 +15,7 @@ app.use(express.static('public'));
app.use('/api/data', data);
app.use('/api/upload', upload);
app.use('/api/save', save);
app.use('/api/file', file);
app.listen(port, () => {
console.log(`Listening on port: ${port}`);