forked from sheetjs/sheetjs
Refresh server demos
- microjs demo removed - hapi.js demo does not work in latest version
This commit is contained in:
parent
1d7aff45c8
commit
e958dbf18e
@ -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
|
||||
|
@ -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
|
||||
```
|
||||
|
@ -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");
|
||||
};
|
19
demos/server/sheetjs.csv
Normal file
19
demos/server/sheetjs.csv
Normal file
@ -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
|
|
Loading…
Reference in New Issue
Block a user