uppy/examples/xhr-node/server.js
Merlijn Vos 7a50ab89ef
Cleanup examples (#5817)
- Remove outdated examples
- Rename folders for consistency
- Remove lots of unused deps in angular example.
- Rename `name` inside `package.json`'s from `@uppy-example/*` to
`example-*`.

**Before**

```
├── angular-example
├── aws-companion
├── aws-nodejs
├── aws-php
├── bundled
├── cdn-example
├── custom-provider
├── digitalocean-spaces
├── multiple-instances
├── node-xhr
├── php-xhr
├── python-xhr
├── react
├── react-native-expo
├── redux
├── sveltekit
├── transloadit
├── transloadit-markdown-bin
├── uppy-with-companion
├── vue3
└── xhr-bundle
```

**After**

```
examples
├── angular
├── aws-companion
├── aws-nodejs
├── aws-php
├── cdn
├── companion
├── companion-custom-provider
├── companion-digitalocean-spaces
├── react
├── react-native-expo
├── redux
├── sveltekit
├── transloadit
├── vue
├── xhr-bundle
├── xhr-node
├── xhr-php
└── xhr-python
```
2025-07-10 17:23:58 +02:00

59 lines
1.5 KiB
JavaScript
Executable file

#!/usr/bin/env node
import { mkdir } from 'node:fs/promises'
import http from 'node:http'
import { fileURLToPath } from 'node:url'
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 {
file: [{ 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(3020, () => {
console.log('server started')
})