Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import express from 'express';
- import dotenv from 'dotenv';
- import * as path from 'path';
- import multer from 'multer';
- import bodyParser from 'body-parser';
- import axios from 'axios';
- import FormData from 'form-data';
- const baseImagePath = path.join(__dirname, '../', 'images')
- const ibbAPIkey = '6cbbe4f41c6a665ed39a66645f2c7bcb'
- const profilePhotoPath = path.join(baseImagePath, 'user_profile_photos')
- const storage = multer.memoryStorage()
- const app = express();
- const upload = multer({storage:storage })
- dotenv.config();
- app.use(bodyParser.json())
- app.use(bodyParser.urlencoded({extended: true}))
- app.get('/', (req, res) => {
- res.send('<h1>Hello World</h1>');
- });
- const filePath = path.join(__dirname, '../', 'images', 'user_cover_photos')
- app.get('/image', (req, res)=>{
- res.sendFile(path.join(filePath, 'shawon majid2.png'))
- })
- app.post('/upload', upload.single('image'), async (req, res) => {
- try {
- const form = new FormData();
- form.append('key', ibbAPIkey);
- form.append('image', req.file?.buffer, { filename: 'image.png' });
- const ibbResponse = await axios.post('https://api.imgbb.com/1/upload', form, {
- headers: form.getHeaders(),
- });
- console.log(ibbResponse.data.data.image.url);
- res.send('File uploaded successfully');
- } catch (error) {
- console.log(error)
- res.status(500).send('Failed to upload');
- }
- });
- app.listen(process.env.PORT, () => {
- console.log('Server is listening on port: ' + process.env.PORT + '...');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement