/
server.js
63 lines (53 loc) · 2.14 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const fs = require('fs')
const path = require('path')
const budo = require('budo')
const router = require('router')
const companion = require('../../packages/@uppy/companion')
/**
* Environment variables:
*
* - COMPANION_AWS_REGION - Your space region, eg "ams3"
* - COMPANION_AWS_KEY - Your access key ID
* - COMPANION_AWS_SECRET - Your secret access key
* - COMPANION_AWS_BUCKET - Your space's name.
*/
if (!process.env.COMPANION_AWS_REGION) throw new Error('Missing Space region, please set the COMPANION_AWS_REGION environment variable (eg. "COMPANION_AWS_REGION=ams3")')
if (!process.env.COMPANION_AWS_KEY) throw new Error('Missing access key, please set the COMPANION_AWS_KEY environment variable')
if (!process.env.COMPANION_AWS_SECRET) throw new Error('Missing secret key, please set the COMPANION_AWS_SECRET environment variable')
if (!process.env.COMPANION_AWS_BUCKET) throw new Error('Missing Space name, please set the COMPANION_AWS_BUCKET environment variable')
// Prepare the server.
const PORT = process.env.PORT || 3452
const app = router()
// Set up the /params endpoint that will create signed URLs for us.
app.use(require('cors')())
app.use(require('body-parser').json())
const { app: companionApp } = companion.app({
providerOptions: {
s3: {
// This is the crucial part; set an endpoint template for the service you want to use.
endpoint: 'https://{region}.digitaloceanspaces.com',
getKey: (req, filename) => `uploads/${filename}`,
key: process.env.COMPANION_AWS_KEY,
secret: process.env.COMPANION_AWS_SECRET,
bucket: process.env.COMPANION_AWS_BUCKET,
region: process.env.COMPANION_AWS_REGION,
},
},
server: { serverUrl: `localhost:${PORT}` },
})
app.use('/companion', companionApp)
// Serve the built CSS file.
app.get('/uppy.min.css', (req, res) => {
res.setHeader('content-type', 'text/css')
fs.createReadStream(path.join('../../packages/uppy/dist/uppy.min.css')).pipe(res)
})
// Start the development server, budo.
budo(path.join(__dirname, 'main.js'), {
live: true,
stream: process.stdout,
port: PORT,
middleware: app,
browserify: {
transform: ['babelify'],
},
})