server.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. app.use('/companion', 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. // Serve the built CSS file.
  39. app.get('/uppy.min.css', (req, res) => {
  40. res.setHeader('content-type', 'text/css')
  41. fs.createReadStream(path.join('../../packages/uppy/dist/uppy.min.css')).pipe(res)
  42. })
  43. // Start the development server, budo.
  44. budo(path.join(__dirname, 'main.js'), {
  45. live: true,
  46. stream: process.stdout,
  47. port: PORT,
  48. middleware: app,
  49. browserify: {
  50. transform: [
  51. 'babelify',
  52. ['aliasify', {
  53. aliases: {
  54. '@uppy': path.join(__dirname, '../../packages/@uppy')
  55. }
  56. }]
  57. ]
  58. }
  59. })