[Node.js] 상위 경로 module.exports = path.dirname(require.main.filename)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const path = require('path');   // 파일 폴더 가져오기
 
const express = require('express'); // 서버 생성
 
const root = require('../util/path'); // 상위폴더가기
 
const router = express.Router(); // 서버 라우터 생성
 
router.get('/',(req,res,next)=>{        
    console.log('여기로 넘어와랑');  
    res.sendFile(path.join(root,'views','shop.html'));       // 절대경로를 고정시켜주는것 path.join(__dirname), _dirname=router 폴더의미
})
 
module.exports= router;
 
 
 
 
-------------------------- admin ------------
 
 
const path = require('path');
 
const express = require('express'); // 서버 생성
 
const router = express.Router(); // 라우터 호출
 
const root = require('../util/path')
 
// /admin/add-product -=> GET 방식
router.get('/add-product',(req,res,next)=>{    // server.get >> router.get으로 변경     
    console.log('여기로 넘어와랑');  
    res.sendFile(path.join(root,'views','add-product.html'));
    res.send('<html><form action="/admin/add-product" method="post"><input type="text" name="title"><button type="submit">상품</button></form></html>')
})
 
 
//  /admin/add-product -- > post 방식
router.post('/add-product',(req,res,next)=>// server.get >> router.get으로 변경  
 
    console.log(req.body); 
                           
 
    res.redirect('/');
})
 
module.exports = router;  // router 들을 내보낸다.
 
-------------------------- utill path.js
 
const path = require('path');
 
module.exports= path.dirname(require.main.filename);   //디렉토리 회신
cs