Skip to content

Commit

Permalink
NestJS/SheetJS demo instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlo committed Feb 26, 2022
1 parent d50a781 commit 56e23ca
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ tmp
.eslintignore
.eslintrc
.jshintrc
xlsx.mini.js
CONTRIBUTING.md
Makefile
make.cmd
Expand Down
1 change: 1 addition & 0 deletions demos/server/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
xlsx.full.min.js
xlsx-demo
4 changes: 4 additions & 0 deletions demos/server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 34 additions & 2 deletions demos/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
```
```



Expand Down Expand Up @@ -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 changes: 24 additions & 0 deletions demos/server/nest.sh
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions demos/server/sheetjs.controller.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 13 additions & 0 deletions demos/server/sheetjs.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}

0 comments on commit 56e23ca

Please sign in to comment.