박민정

[feat] Store the image to server

...@@ -30,9 +30,9 @@ app.use(cors()) ...@@ -30,9 +30,9 @@ app.use(cors())
30 30
31 31
32 app.use('/api/users', require('./routes/users')); 32 app.use('/api/users', require('./routes/users'));
33 +app.use('/api/image', require('./routes/productImage'));
33 34
34 - 35 +// 업로드 하려고
35 -// 이미지 가져오려고
36 app.use('/uploads', express.static('uploads')); 36 app.use('/uploads', express.static('uploads'));
37 37
38 if (process.env.NODE_ENV === "production") { 38 if (process.env.NODE_ENV === "production") {
...@@ -42,6 +42,7 @@ if (process.env.NODE_ENV === "production") { ...@@ -42,6 +42,7 @@ if (process.env.NODE_ENV === "production") {
42 }); 42 });
43 } 43 }
44 44
45 +
45 app.listen(port, () => { 46 app.listen(port, () => {
46 console.log(`Server ---> http://localhost:${port}`) 47 console.log(`Server ---> http://localhost:${port}`)
47 }); 48 });
...\ No newline at end of file ...\ No newline at end of file
......
1 +const express = require('express');
2 +const { User } = require("../models/User");
3 +const { auth } = require("../middleware/auth");
4 +const router = express.Router();
5 +
6 +
7 +var storage = multer.diskStorage({
8 + destination: function (req, file, cb) {
9 + cb(null, 'uploads/') // 어느 폴더에 저장할건지
10 + },
11 + filename: function (req, file, cb) {
12 + cb(null, Date.now() + '_' + file.originalname) // 이미지 이름
13 + }
14 + })
15 +
16 + var upload = multer({ storage: storage })
17 +
18 +router.post('/image', (req, res) => {
19 +
20 + // 클라이언트로부터 받은 이미지 저장
21 + upload(req, res, err => {
22 + if (err) return req.json({ success: false, err })
23 + return res.json({success: true, filePath: res.req.file.path, fileName: res.req.file.filename})
24 + }
25 + )
26 +
27 +})
28 +
29 +module.exports = router;