Skip to content

Commit 59b4e50

Browse files
authored
Video-24-Upload-Image-Local-Server (#62)
1 parent ffa716a commit 59b4e50

5 files changed

Lines changed: 279 additions & 133 deletions

File tree

README.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,50 @@ This part shows list of reviews by users for each products. also it provides a f
179179
29. save review in product.reviews
180180
30. update avg rating
181181

182-
### Part 25- Upload images
183-
184-
Admin shoud be able to uploads photos from their computer. This section teaches this feature.
182+
### Part 25- Upload Product Images On Local Server
183+
184+
Admin shoud be able to uploads photos from their computer. This section is about uploading images on local server ans aws s3 cloud server.
185+
186+
1. npm install multer
187+
2. routes/uploadRoute.js
188+
3. import express and multer
189+
4. create disk storage with Date.now().jpg as filename
190+
5. set upload as multer({ storage })
191+
6. router.post('/', upload.single('image'))
192+
7. return req.file.path
193+
8. server.js
194+
9. app.use('/api/uploads',uploadRoute)
195+
10. ProductsScreen.js
196+
11. create state hook for uploading
197+
12. create input image file and onChange handler
198+
13. define handleUploadImage function
199+
14. prepare file for upload
200+
15. axios post file as multipart/form-data
201+
16. set image and set uploading
202+
17. check result
203+
204+
### Part 26- Upload Product Images On AWS S3
205+
206+
This section is about uploading images amazon aws s3 cloud server.
207+
208+
1. create aws account
209+
2. open https://s3.console.aws.amazon.com
210+
3. create public bucket as amazona
211+
4. create api key and secret
212+
5. past it into .env as accessKeyId and secretAccessKey
213+
6. move dotenv to config.js
214+
7. add accessKeyId and secretAccessKey to config.js
215+
8. npm install aws-sdk multer-s3
216+
9. routes/uploadRoute.js
217+
10. set aws.config.update to config values
218+
11. create s3 from new aws.S3()
219+
12. create storageS3 from multerS3 by setting s3, bucket and acl
220+
13. set uploadS3 as multer({ storage: storageS3 })
221+
14. router.post('/s3', uploadS3.single('image'))
222+
15. return req.file.location
223+
16. ProductsScreen.js
224+
17. on handleUploadImage set axios.post('api/uploads/s3')
225+
18. check result on website and s3
185226

186227
## Summary
187228

backend/routes/uploadRoute.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import express from 'express';
2+
import multer from 'multer';
3+
4+
const storage = multer.diskStorage({
5+
destination(req, file, cb) {
6+
cb(null, 'uploads/');
7+
},
8+
filename(req, file, cb) {
9+
cb(null, `${Date.now()}.jpg`);
10+
},
11+
});
12+
13+
const upload = multer({ storage });
14+
15+
const router = express.Router();
16+
17+
router.post('/', upload.single('image'), (req, res) => {
18+
res.send(`/${req.file.path}`);
19+
});
20+
export default router;

backend/server.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,32 @@ import config from './config';
66
import userRoute from './routes/userRoute';
77
import productRoute from './routes/productRoute';
88
import orderRoute from './routes/orderRoute';
9+
import uploadRoute from './routes/uploadRoute';
910

1011
const mongodbUrl = config.MONGODB_URL;
11-
mongoose.connect(mongodbUrl, {
12-
useNewUrlParser: true,
13-
useUnifiedTopology: true,
14-
useCreateIndex: true,
15-
}).catch((error) => console.log(error.reason));
16-
12+
mongoose
13+
.connect(mongodbUrl, {
14+
useNewUrlParser: true,
15+
useUnifiedTopology: true,
16+
useCreateIndex: true,
17+
})
18+
.catch((error) => console.log(error.reason));
1719

1820
const app = express();
1921
app.use(bodyParser.json());
20-
22+
app.use('/api/uploads', uploadRoute);
2123
app.use('/api/users', userRoute);
2224
app.use('/api/products', productRoute);
2325
app.use('/api/orders', orderRoute);
2426
app.get('/api/config/paypal', (req, res) => {
2527
res.send(config.PAYPAL_CLIENT_ID);
2628
});
27-
29+
app.use('/uploads', express.static(path.join(__dirname, '/../uploads')));
2830
app.use(express.static(path.join(__dirname, '/../frontend/build')));
2931
app.get('*', (req, res) => {
3032
res.sendFile(path.join(`${__dirname}/../frontend/build/index.html`));
3133
});
3234

33-
app.listen(config.PORT, () => { console.log('Server started at http://localhost:5000'); });
35+
app.listen(config.PORT, () => {
36+
console.log('Server started at http://localhost:5000');
37+
});

0 commit comments

Comments
 (0)