Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example: fix svelte example #4017

Merged
merged 1 commit into from Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/svelte-example/.gitignore
@@ -1,4 +1,5 @@
/node_modules/
/uploads/
/public/build/

.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion examples/svelte-example/README.md
Expand Up @@ -12,5 +12,5 @@ corepack yarn build
Then, again in the **repository root**, start this example by doing:

```sh
corepack yarn workspace @uppy-example/svelte-app dev
corepack yarn workspace @uppy-example/svelte-app start
```
7 changes: 5 additions & 2 deletions examples/svelte-example/package.json
Expand Up @@ -3,8 +3,9 @@
"version": "0.0.0",
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public",
"start:client": "rollup -c -w",
"start:server": "node ./server.mjs",
"start": "npm-run-all --parallel start:client start:server",
"validate": "svelte-check"
},
"devDependencies": {
Expand All @@ -13,6 +14,7 @@
"@rollup/plugin-node-resolve": "^13.0.0",
"@rollup/plugin-typescript": "^8.0.0",
"@tsconfig/svelte": "^1.0.0",
"npm-run-all": "^4.1.5",
"postcss": "^8.2.1",
"postcss-import": "^13.0.0",
"postcss-load-config": "^3.0.0",
Expand All @@ -32,6 +34,7 @@
"@uppy/svelte": "workspace:*",
"@uppy/webcam": "workspace:*",
"@uppy/xhr-upload": "workspace:*",
"formidable": "^2.0.1",
"sirv-cli": "^1.0.0"
},
"private": true
Expand Down
52 changes: 52 additions & 0 deletions examples/svelte-example/server.mjs
@@ -0,0 +1,52 @@
#!/usr/bin/env node

/* eslint-disable no-console */

import http from 'node:http'
import { fileURLToPath } from 'node:url'
import { mkdir } from 'node:fs/promises'

import formidable from 'formidable'

const UPLOAD_DIR = new URL('./uploads/', import.meta.url)

await mkdir(UPLOAD_DIR, { recursive: true })

http.createServer((req, res) => {
const headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS, POST, GET',
'Access-Control-Max-Age': 2592000, // 30 days
/** add other headers as per requirement */
}

if (req.method === 'OPTIONS') {
res.writeHead(204, headers)
res.end()
return
}
if (req.url === '/upload' && req.method.toLowerCase() === 'post') {
// parse a file upload
const form = formidable({
keepExtensions: true,
uploadDir: fileURLToPath(UPLOAD_DIR),
})

form.parse(req, (err, fields, files) => {
if (err) {
console.log('some error', err)
res.writeHead(200, headers)
res.write(JSON.stringify(err))
return res.end()
}
const { files: { filepath, originalFilename, mimetype, size } } = files
console.log('saved file', { filepath, originalFilename, mimetype, size })
res.writeHead(200, headers)
res.write(JSON.stringify({ fields, files }))
return res.end()
})
}
}).listen(9967, () => {
console.log('server started')
})
2 changes: 2 additions & 0 deletions yarn.lock
Expand Up @@ -8246,6 +8246,8 @@ __metadata:
"@uppy/svelte": "workspace:*"
"@uppy/webcam": "workspace:*"
"@uppy/xhr-upload": "workspace:*"
formidable: ^2.0.1
npm-run-all: ^4.1.5
postcss: ^8.2.1
postcss-import: ^13.0.0
postcss-load-config: ^3.0.0
Expand Down