Skip to content

Commit

Permalink
feat(example-file-upload): add an example for file upload with LoopBa…
Browse files Browse the repository at this point in the history
…ck 4
  • Loading branch information
raymondfeng committed Mar 7, 2020
1 parent 6fd3bf1 commit ab3da3e
Show file tree
Hide file tree
Showing 26 changed files with 2,203 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/file-upload/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=true
2 changes: 2 additions & 0 deletions examples/file-upload/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
*.json
6 changes: 6 additions & 0 deletions examples/file-upload/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"bracketSpacing": false,
"singleQuote": true,
"printWidth": 80,
"trailingComma": "all"
}
32 changes: 32 additions & 0 deletions examples/file-upload/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"editor.rulers": [80],
"editor.tabCompletion": "on",
"editor.tabSize": 2,
"editor.trimAutoWhitespace": true,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true,
"source.fixAll.eslint": true
},

"files.exclude": {
"**/.DS_Store": true,
"**/.git": true,
"**/.hg": true,
"**/.svn": true,
"**/CVS": true,
"dist": true,
},
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,

"typescript.tsdk": "./node_modules/typescript/lib",
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": false,
"typescript.preferences.quoteStyle": "single",
"eslint.run": "onSave",
"eslint.nodePath": "./node_modules",
"eslint.validate": [
"javascript",
"typescript"
]
}
29 changes: 29 additions & 0 deletions examples/file-upload/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Watch and Compile Project",
"type": "shell",
"command": "npm",
"args": ["--silent", "run", "build:watch"],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$tsc-watch"
},
{
"label": "Build, Test and Lint",
"type": "shell",
"command": "npm",
"args": ["--silent", "run", "test:dev"],
"group": {
"kind": "test",
"isDefault": true
},
"problemMatcher": ["$tsc", "$eslint-compact", "$eslint-stylish"]
}
]
}
68 changes: 68 additions & 0 deletions examples/file-upload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# @loopback/example-file-upload

An example application to demonstrate file uploads for LoopBack 4

## Summary

This application comes with a controller that exposes `/file-upload` endpoint
that accepts `multipart/form-data` based file uploads.

## Key artifacts

- [FileUploadController](src/controllers/file-upload.controller.ts)

- Expose `/file-upload` endpoint to allow file uploads

- [FileUploadService - an Express middleware from multer](src/services/file-upload.service.ts)

- A service provider that returns a configured `multer` request handler

The file upload is configured with `multer` options in
[src/application.ts](src/application.ts) as follows:

```ts
// Configure file upload with multer options
const multerOptions: multer.Options = {
storage: multer.diskStorage({
// Upload files to `.sandbox`
destination: path.join(__dirname, '../.sandbox'),
// Use the original file name as is
filename: (req, file, cb) => {
cb(null, file.originalname);
},
}),
};
this.configure(FILE_UPLOAD_SERVICE).to(multerOptions);
```

## Use

Start the app:

```sh
npm start
```

The application will start on port `3000`. Open http://localhost:3000 in your
browser. You can try to upload a few files using the web UI or API explorer.

The uploaded files will be stored in `.sandbox` folder under the application
root directory.

## Contributions

- [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md)
- [Join the team](https://github.com/strongloop/loopback-next/issues/110)

## Tests

Run `npm test` from the root folder.

## Contributors

See
[all contributors](https://github.com/strongloop/loopback-next/graphs/contributors).

## License

MIT
8 changes: 8 additions & 0 deletions examples/file-upload/fixtures/assets/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<header>
<title>Test Page</title>
</header>
<body>
<h1>Hello, World!</h1>
</body>
</html>
1 change: 1 addition & 0 deletions examples/file-upload/fixtures/file-upload-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is used for file-upload acceptance test.
32 changes: 32 additions & 0 deletions examples/file-upload/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright IBM Corp. 2020. All Rights Reserved.
// Node module: @loopback/example-file-upload
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const application = require('./dist');

module.exports = application;

if (require.main === module) {
// Run the application
const config = {
rest: {
port: +(process.env.PORT || 3000),
host: process.env.HOST,
// The `gracePeriodForClose` provides a graceful close for http/https
// servers with keep-alive clients. The default value is `Infinity`
// (don't force-close). If you want to immediately destroy all sockets
// upon stop, set its value to `0`.
// See https://www.npmjs.com/package/stoppable
gracePeriodForClose: 5000, // 5 seconds
openApiSpec: {
// useful when used with OpenAPI-to-GraphQL to locate your application
setServersFromRequest: true,
},
},
};
application.main(config).catch(err => {
console.error('Cannot start the application.', err);
process.exit(1);
});
}
6 changes: 6 additions & 0 deletions examples/file-upload/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2020. All Rights Reserved.
// Node module: @loopback/example-file-upload
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './src';
Loading

0 comments on commit ab3da3e

Please sign in to comment.