NestJS/SheetJS demo instructions

This commit is contained in:
Dan Mlodecki 2022-02-26 01:48:40 -04:00
parent d50a7812c4
commit 56e23ca8e0
7 changed files with 96 additions and 2 deletions

@ -43,6 +43,7 @@ tmp
.eslintignore
.eslintrc
.jshintrc
xlsx.mini.js
CONTRIBUTING.md
Makefile
make.cmd

@ -1 +1,2 @@
xlsx.full.min.js
xlsx-demo

@ -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

@ -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)

24
demos/server/nest.sh Executable file

@ -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

@ -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;
}
}

@ -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 {}