Advertisement
shawon_majid

Untitled

Dec 22nd, 2023
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import express from 'express';
  2. import dotenv from 'dotenv';
  3. import * as path from 'path';
  4. import multer from 'multer';
  5. import bodyParser from 'body-parser';
  6. import axios from 'axios';
  7. import FormData from 'form-data';
  8.  
  9.  
  10. const baseImagePath =  path.join(__dirname, '../', 'images')
  11.  
  12. const ibbAPIkey = '6cbbe4f41c6a665ed39a66645f2c7bcb'
  13.  
  14. const profilePhotoPath = path.join(baseImagePath, 'user_profile_photos')
  15. const storage = multer.memoryStorage()
  16.  
  17. const app = express();
  18. const upload = multer({storage:storage })
  19. dotenv.config();
  20. app.use(bodyParser.json())
  21. app.use(bodyParser.urlencoded({extended: true}))
  22. app.get('/', (req, res) => {
  23.     res.send('<h1>Hello World</h1>');
  24. });
  25.  
  26. const filePath = path.join(__dirname, '../', 'images', 'user_cover_photos')
  27.  
  28. app.get('/image', (req, res)=>{
  29.     res.sendFile(path.join(filePath, 'shawon majid2.png'))
  30. })
  31.  
  32. app.post('/upload', upload.single('image'), async (req, res) => {
  33.   try {
  34.     const form = new FormData();
  35.     form.append('key', ibbAPIkey);
  36.     form.append('image', req.file?.buffer, { filename: 'image.png' });
  37.  
  38.     const ibbResponse = await axios.post('https://api.imgbb.com/1/upload', form, {
  39.       headers: form.getHeaders(),
  40.     });
  41.  
  42.     console.log(ibbResponse.data.data.image.url);
  43.     res.send('File uploaded successfully');
  44.   } catch (error) {
  45.     console.log(error)
  46.     res.status(500).send('Failed to upload');
  47.   }
  48. });
  49.  
  50. app.listen(process.env.PORT, () => {
  51.     console.log('Server is listening on port: ' + process.env.PORT + '...');
  52. });
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement