From e958dbf18e6c21986c74692d71a1a4205e81a061 Mon Sep 17 00:00:00 2001 From: AmishBaztard <49459941+AmishBaztard@users.noreply.github.com> Date: Thu, 15 Apr 2021 22:09:55 -0400 Subject: [PATCH] Refresh server demos - microjs demo removed - hapi.js demo does not work in latest version --- demos/server/Makefile | 5 -- demos/server/README.md | 59 +++++++++++++------- demos/server/micro.js | 85 ----------------------------- demos/server/{node.js => nodejs.js} | 0 demos/server/sheetjs.csv | 19 +++++++ 5 files changed, 59 insertions(+), 109 deletions(-) delete mode 100644 demos/server/micro.js rename demos/server/{node.js => nodejs.js} (100%) create mode 100644 demos/server/sheetjs.csv diff --git a/demos/server/Makefile b/demos/server/Makefile index a293e57..a853f6a 100644 --- a/demos/server/Makefile +++ b/demos/server/Makefile @@ -1,6 +1,5 @@ .PHONY: init init: - if [ ! -e sheetjs.xlsx ]; then ln -s ../../sheetjs.xlsx; fi mkdir -p node_modules cd node_modules; if [ ! -e xlsx ]; then ln -s ../../../ xlsx; fi; cd - @@ -12,10 +11,6 @@ request: init ## request demo express: init ## express demo node express.js -.PHONY: micro -micro: init ## micro demo - micro -p 7262 micro.js - .PHONY: koa koa: init ## koa demo node koa.js diff --git a/demos/server/README.md b/demos/server/README.md index 99308bf..a23e545 100644 --- a/demos/server/README.md +++ b/demos/server/README.md @@ -8,6 +8,38 @@ demo shows a few different strategies applied to different server frameworks. NOTE: these examples merely demonstrate the core concepts and do not include appropriate error checking or other production-level features. + +### Express Setup + +The following commands are required in order to test the [Express](https://github.com/expressjs/express) demo: + +```bash +npm install express printj xlsx express-formidable +node express.js +``` + +### Koa Setup + +The following commands are required in order to test the [Koa](https://github.com/koajs/koa) demo: + +```bash +npm install koa printj formidable xlsx +node koa.js +``` + +### Hapi Setup + +**Note: Hapi demo as written only works with Hapi version 16 and below.** + +The following commands are required in order to test the [Hapi](https://github.com/hapijs/hapi) demo: + +```bash +npm install hapi@16.x printj tiny-worker xlsx +node hapi.js +``` + + + ### Node Buffer The `read` and `write` functions can handle `Buffer` data with `type:"buffer"`. @@ -64,12 +96,12 @@ expected to handle: Testing with cURL is straightforward: ```bash -# upload test.xls and update data -curl -X POST -F "data=@test.xls" http://localhost:7262/ +# upload sheetjs.csv and update data +curl -X POST -F "data=@sheetjs.csv" http://localhost:7262/ # download data in SYLK format curl -X GET http://localhost:7262/?t=slk -# read sheetjs.xlsx from the server directory -curl -X POST http://localhost:7262/?f=sheetjs.xlsx +# read sheetjs.csv from the server directory +curl -X POST http://localhost:7262/?f=sheetjs.csv # write sheetjs.xlsb in the XLSB format curl -X GET http://localhost:7262/?f=sheetjs.xlsb ``` @@ -108,17 +140,6 @@ The main server script is `koa.js` and the worker script is `koasub.js`. State is maintained in the worker script. -## command-line utility with micro - -The npm module ships with the `xlsx` command line tool. For global installs, the -script `bin/xlsx.njs` is added to a directory in `PATH`. For local installs, the -appropriate script or symbolic link is set up in `node_modules/.bin/`. - -The `--arrays` option directs `xlsx` to generate an array of arrays that can be -parsed by the server. To generate files, the `json2csv` module exports the JS -array of arrays to a CSV, the server writes the file, and the `xlsx` command is -used to generate files of different formats. - ## tiny-worker with hapi @@ -132,12 +153,12 @@ Note: due to an issue with hapi payload parsing, the route `POST /file` is used to handle the case of reading from file, so the cURL test is: ```bash -# upload test.xls and update data -curl -X POST -F "data=@test.xls" http://localhost:7262/ +# upload sheetjs.csv and update data +curl -X POST -F "data=@sheetjs.csv" http://localhost:7262/ # download data in SYLK format curl -X GET http://localhost:7262/?t=slk -# read sheetjs.xlsx from the server directory -curl -X POST http://localhost:7262/file?f=sheetjs.xlsx +# read sheetjs.csv from the server directory +curl -X POST http://localhost:7262/file?f=sheetjs.csv # write sheetjs.xlsb in the XLSB format curl -X GET http://localhost:7262/?f=sheetjs.xlsb ``` diff --git a/demos/server/micro.js b/demos/server/micro.js deleted file mode 100644 index 8163247..0000000 --- a/demos/server/micro.js +++ /dev/null @@ -1,85 +0,0 @@ -/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */ -var fs = require('fs'); -var URL = require('url'); -var child_process = require('child_process'); -var micro = require('micro'), formidable = require('formidable'); -var logit = require('./_logit'), cors = require('./_cors'); -var json2csv = require('json2csv'); -var data = "a,b,c\n1,2,3".split("\n").map(function(x) { return x.split(","); }); -var xlsx = '../../bin/xlsx.njs'; - -function get_data(req, res, type) { - var file = "_tmp." + type; - - /* prepare CSV */ - var csv = json2csv({data:data, hasCSVColumnTitle:false}); - - /* write it to a temp file */ - fs.writeFile('tmp.csv', csv, function(err1) { - - /* call xlsx to read the csv and write to another temp file */ - child_process.exec(xlsx+' tmp.csv -o '+ file, function(err, stdout, stderr){ - cors(req, res); - /* read the new file and send it to the client */ - micro.send(res, 200, fs.readFileSync(file)); - }); - }); -} - -function get_file(req, res, file) { - var csv = json2csv({data:data, hasCSVColumnTitle:false}); - fs.writeFile('tmp.csv', csv, function(err1) { - /* write to specified file */ - child_process.exec(xlsx+' tmp.csv -o '+file, function(err, stdout, stderr) { - cors(req, res); - micro.send(res, 200, "wrote to " + file + "\n"); - }); - }); -} - -function post_data(req, res) { - var form = new formidable.IncomingForm(); - form.on('file', function(field, file) { - /* file.path is the location of the file in the system */ - child_process.exec(xlsx+' --arrays ' + file.path, post_cb(req, res)); - }); - form.parse(req); -} - -function post_file(req, res, file) { - child_process.exec(xlsx+' --arrays ' + file, post_cb(req, res)); -} - -function post_cb(req, res) { - return function(err, stdout, stderr) { - cors(req, res); - /* xlsx --arrays writes JSON to stdout, so parse and assign to data var */ - data = JSON.parse(stdout); - console.log(data); - return micro.send(res, 200, "ok\n"); - }; -} - -function get(req, res) { - var url = URL.parse(req.url, true); - if(url.pathname.length > 1) micro.send(res, 404, "File not found"); - else if(url.query.t) get_data(req, res, url.query.t); - else if(url.query.f) get_file(req, res, url.query.f); - else micro.send(res, 403, "Forbidden\n"); -} - -function post(req, res) { - var url = URL.parse(req.url, true); - if(url.pathname.length > 1) micro.send(res, 404, "File not found"); - else if(url.query.f) post_file(req, res, url.query.f); - else post_data(req, res); -} - -module.exports = function(req, res) { - logit(req, res); - switch(req.method) { - case 'GET': return get(req, res); - case 'POST': return post(req, res); - } - return micro.send(res, 501, "Unsupported method " + req.method + "\n"); -}; diff --git a/demos/server/node.js b/demos/server/nodejs.js similarity index 100% rename from demos/server/node.js rename to demos/server/nodejs.js diff --git a/demos/server/sheetjs.csv b/demos/server/sheetjs.csv new file mode 100644 index 0000000..eddbf8a --- /dev/null +++ b/demos/server/sheetjs.csv @@ -0,0 +1,19 @@ +Text,Number,Rich,Span +This is Bold,123,This is Bold,This is Bold +This is Italic,234,This is Italic,This is Italic +This is Underline,345,This is Underline,This is Underline +This is Stricken,456,This is Stricken,This is Stricken +This is 18 px,567,This is 18 px,This is 18 px +This is superscript,678,This is superscript,This is superscript +This is subscript,789,This is subscript,This is subscript +This is red,135,This is red,This is red +This is green,246,This is green,This is green +This is Times,357,This is Times,This is Times +This is BIU,159,This is 01324576 yes,This is BIU +BG Green,255,White on Blue,Green on Black +Standard Newline,W S,"BR +New line","Pre +New line" +Height,100,px (not pt),yeah +Top Left,80,Middle Center,Bottom Right +Top Right,60,Bottom Center,Bottom Left