server.js 2.1 KB

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