index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. require('dotenv').config({ path: path.join(__dirname, '..', '..', '.env') })
  2. const express = require('express')
  3. const app = express()
  4. const path = require('node:path')
  5. const port = process.env.PORT
  6. const bodyParser = require('body-parser')
  7. const aws = require('aws-sdk')
  8. app.use(bodyParser.json())
  9. app.get('/', (req, res) => {
  10. const htmlPath = path.join(__dirname, 'public', 'index.html')
  11. res.sendFile(htmlPath)
  12. })
  13. app.get('/drag', (req, res) => {
  14. const htmlPath = path.join(__dirname, 'public', 'drag.html')
  15. res.sendFile(htmlPath)
  16. })
  17. app.post('/sign-s3', (req, res) => {
  18. const s3 = new aws.S3()
  19. const fileName = req.body.filename
  20. const { contentType } = req.body
  21. const s3Params = {
  22. Bucket: process.env.S3_BUCKET,
  23. Key: fileName,
  24. Expires: 60,
  25. ContentType: contentType,
  26. ACL: 'public-read',
  27. }
  28. s3.getSignedUrl('putObject', s3Params, (err, data) => {
  29. if (err) {
  30. console.log(err)
  31. return res.end()
  32. }
  33. const returnData = {
  34. url: data,
  35. method: 'PUT',
  36. }
  37. res.write(JSON.stringify(returnData))
  38. res.end()
  39. })
  40. })
  41. app.listen(port, () => {
  42. console.log(`Example app listening on port ${port}`)
  43. })