From 56e23ca8e09e5513a8214c9d39d11a0366353daf Mon Sep 17 00:00:00 2001 From: Dan Mlodecki Date: Sat, 26 Feb 2022 01:48:40 -0400 Subject: [PATCH] NestJS/SheetJS demo instructions --- .npmignore | 1 + demos/server/.gitignore | 1 + demos/server/Makefile | 4 ++++ demos/server/README.md | 36 ++++++++++++++++++++++++++++-- demos/server/nest.sh | 24 ++++++++++++++++++++ demos/server/sheetjs.controller.ts | 19 ++++++++++++++++ demos/server/sheetjs.module.ts | 13 +++++++++++ 7 files changed, 96 insertions(+), 2 deletions(-) create mode 100755 demos/server/nest.sh create mode 100644 demos/server/sheetjs.controller.ts create mode 100644 demos/server/sheetjs.module.ts diff --git a/.npmignore b/.npmignore index fea128d..c28e5c2 100644 --- a/.npmignore +++ b/.npmignore @@ -43,6 +43,7 @@ tmp .eslintignore .eslintrc .jshintrc +xlsx.mini.js CONTRIBUTING.md Makefile make.cmd diff --git a/demos/server/.gitignore b/demos/server/.gitignore index 082cff2..0bb4e01 100644 --- a/demos/server/.gitignore +++ b/demos/server/.gitignore @@ -1 +1,2 @@ xlsx.full.min.js +xlsx-demo diff --git a/demos/server/Makefile b/demos/server/Makefile index a853f6a..b591450 100644 --- a/demos/server/Makefile +++ b/demos/server/Makefile @@ -19,3 +19,7 @@ koa: init ## koa demo hapi: init ## hapi demo cp ../../dist/xlsx.full.min.js . node hapi.js + +.PHONY: nest +nest: init ## nest demo + bash -c ./nest.sh diff --git a/demos/server/README.md b/demos/server/README.md index a23e545..fdf2f69 100644 --- a/demos/server/README.md +++ b/demos/server/README.md @@ -25,7 +25,7 @@ The following commands are required in order to test the [Koa](https://github.co ```bash npm install koa printj formidable xlsx node koa.js -``` +``` ### Hapi Setup @@ -36,7 +36,7 @@ The following commands are required in order to test the [Hapi](https://github.c ```bash npm install hapi@16.x printj tiny-worker xlsx node hapi.js -``` +``` @@ -163,4 +163,36 @@ curl -X POST http://localhost:7262/file?f=sheetjs.csv curl -X GET http://localhost:7262/?f=sheetjs.xlsb ``` + + +## NestJS + +[NestJS](https://nestjs.com/) is a Node.js framework for server-side web applications. + +This demo uses SheetJS to injest a spreadsheet via a POST API endpoint. The file +arrive to the endpoint as body `form-data`, accessible using the `file` key. +After parsing the file, CSV contents of the first worksheet will be returned. +[Body parsing uses `multer`](https://docs.nestjs.com/techniques/file-upload). + +Before running the demo, the NestJS CLI tool must be installed. The instruction +is described in the NestJS ["First Steps"](https://docs.nestjs.com/first-steps): + +```bash +npm i -g @nestjs/cli +make nest +``` + +The demo can be tested using the `/sheetjs/upload-xlsx-file` endpoint: + +```bash +curl -X POST -F "file=@test.xlsx" http://localhost:3000/sheetjs/upload-xlsx-file +``` + +The included [`nest.sh`](./nest.sh) script creates and configures the project. + + +This demo creates a module and a controller. The controller handles the actual +requests (creating the endpoint) while the module is used to configure `multer`. + + [![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/js-xlsx?pixel)](https://github.com/SheetJS/js-xlsx) diff --git a/demos/server/nest.sh b/demos/server/nest.sh new file mode 100755 index 0000000..bd60b62 --- /dev/null +++ b/demos/server/nest.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# it is assumed that @nestjs/cli is installed globally + +if [ ! -e xlsx-demo ]; then + nest new -p npm xlsx-demo +fi + +cd xlsx-demo +npm i --save xlsx +npm i --save-dev @types/multer + +if [ ! -e src/sheetjs/sheetjs.module.ts ]; then + nest generate module sheetjs +fi + +if [ ! -e src/sheetjs/sheetjs.controller.ts ]; then + nest generate controller sheetjs +fi + +cp ../sheetjs.module.ts src/sheetjs/ +cp ../sheetjs.controller.ts src/sheetjs/ +mkdir -p upload +npm run start diff --git a/demos/server/sheetjs.controller.ts b/demos/server/sheetjs.controller.ts new file mode 100644 index 0000000..bad90eb --- /dev/null +++ b/demos/server/sheetjs.controller.ts @@ -0,0 +1,19 @@ +import { Controller, Logger, Post, UploadedFile, UseInterceptors } from '@nestjs/common'; +import { FileInterceptor } from '@nestjs/platform-express'; +import { readFile, utils } from 'xlsx'; + +@Controller('sheetjs') +export class SheetjsController { + private readonly logger = new Logger(SheetjsController.name); + + @Post('upload-xlsx-file') + @UseInterceptors(FileInterceptor('file')) + async uploadXlsxFile(@UploadedFile() file: Express.Multer.File) { + // Open the uploaded XLSX file and perform SheetJS operations + const workbook = readFile(file.path); + const firstSheet = workbook.Sheets[workbook.SheetNames[0]]; + const output = utils.sheet_to_csv(firstSheet); + this.logger.log(output); + return output; + } +} diff --git a/demos/server/sheetjs.module.ts b/demos/server/sheetjs.module.ts new file mode 100644 index 0000000..4dd9a53 --- /dev/null +++ b/demos/server/sheetjs.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common'; +import { SheetjsController } from './sheetjs.controller'; +import { MulterModule } from '@nestjs/platform-express'; + +@Module({ + controllers: [SheetjsController], + imports: [ + MulterModule.register({ + dest: './upload', + }), + ], +}) +export class SheetjsModule {}