Skip to content

Commit e7821e7

Browse files
committed
feat: add expressjs server as example for file upload server handling
1 parent 1c5e2b1 commit e7821e7

File tree

5 files changed

+1273
-0
lines changed

5 files changed

+1273
-0
lines changed

example-express-server/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Example Node.js ExpressJS server
2+
> this is an example to demonstrate the file upload from browser through calling API
3+
4+
## Run server
5+
```bash
6+
yarn install
7+
yarn dev
8+
```
9+
10+
## Run sample frontend
11+
1. Open: https://donaldcwl.github.io/browser-image-compression/example/basic.html
12+
2. Choose a file to compress, then the output will be uploaded to "http://localhost:3000/image-upload-api"
13+
![Sample screenshot](./screenshot.png)
14+
15+
## Sample frontend HTML
16+
```html
17+
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
18+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/browser-image-compression@1.0.15/dist/browser-image-compression.js"></script>
19+
<input type="file" accept="image/*" onchange="compressImage(event);">
20+
<script>
21+
function compressImage (event) {
22+
const file = event.target.files[0]
23+
const options = {
24+
maxSizeMB: 1,
25+
maxWidthOrHeight: 1024,
26+
}
27+
imageCompression(file, options)
28+
.then(output => uploadToServer(output))
29+
.catch(err => console.error(err))
30+
}
31+
32+
function uploadToServer (file) {
33+
const formData = new FormData()
34+
formData.append('image', file, file.name)
35+
const url = 'http://localhost:3000/image-upload-api'
36+
console.log('calling api', url, 'with data', Array.from(formData.entries())[0])
37+
return fetch(url, {
38+
method: 'POST',
39+
body: formData
40+
})
41+
.then(res => res.json())
42+
.then(body => console.log('got server response', body))
43+
}
44+
</script>
45+
```

example-express-server/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const express = require('express')
2+
const multer = require('multer')
3+
const cors = require('cors')
4+
const fs = require('fs')
5+
6+
const UPLOAD_PATH = 'uploads/'
7+
8+
if (!fs.existsSync(UPLOAD_PATH)) {
9+
fs.mkdirSync(UPLOAD_PATH)
10+
}
11+
12+
const app = express()
13+
14+
const storage = multer.diskStorage({
15+
destination: function (req, file, cb) {
16+
cb(null, UPLOAD_PATH)
17+
},
18+
filename: function (req, file, cb) {
19+
cb(null, `${Date.now()}-${file.originalname}`)
20+
}
21+
})
22+
23+
const upload = multer({ storage })
24+
25+
const port = process.env.PORT || 3000
26+
27+
app.use(cors())
28+
29+
app.post('/image-upload-api', upload.single('image'), (req, res) => {
30+
console.log(req.file)
31+
res.json({ success: true, file: req.file })
32+
})
33+
34+
// error handler
35+
app.use((err, req, res, next) => {
36+
console.error(err.stack)
37+
res.status(500).send('Something broke!')
38+
})
39+
40+
const server = app.listen(port, () => {
41+
console.log(`App listening at http://localhost:${port}`)
42+
})
43+
44+
function closeGracefully(signal) {
45+
console.log(`Received signal to terminate: ${signal}`)
46+
47+
server.close(() => {
48+
console.log(`HTTP server closed`)
49+
process.exit()
50+
})
51+
}
52+
53+
process.on('SIGINT', closeGracefully)
54+
process.on('SIGTERM', closeGracefully)

example-express-server/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "example-express-server",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"start": "node .",
8+
"dev": "nodemon ."
9+
},
10+
"dependencies": {
11+
"cors": "^2.8.5",
12+
"express": "^4.17.1"
13+
},
14+
"devDependencies": {
15+
"nodemon": "^2.0.7"
16+
}
17+
}

example-express-server/screenshot.png

428 KB
Loading

0 commit comments

Comments
 (0)