Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 문과 개발
- 7급경제학
- 국내수출
- 9급경제학
- 비전공자 개발
- 자바스크립트
- 경제학적개념
- 경제학적설명
- case method
- 경제학
- 파생금융
- 비전공자 코딩
- 금융기초
- 경제학적기초지식
- 코딩
- 개발자되기
- 배열메소드
- 케이스메소드
- 경제학기초
- 콜드콜
- 파생상품
- 하버드MBA
- 비전공자개발자
- 바닐라자바스크립트
- 개발자
- devlift
- 주식기초
- 파생경제학
- 주식과채권
- 채권기초
Archives
- Today
- Total
deVSner
express - static, bodyparser, cookieparser, session , morgan 본문
개발 일지/node.js (with express)
express - static, bodyparser, cookieparser, session , morgan
RudeofSun 2020. 8. 29. 22:52
1.
const data = await fs.readFile(`.${req.url}`) ~ 이 부분이 static으로 된 거임
app.use('요청경로', express.static('실제경로'));
localhost:3000/zerocho.html learn-express/public-3030/zerocho.html
localhost:3000/zerocho.css learn-express/public-3030/zerocho.css
이 미들웨어는 morgan 밑에 위치시킬 것. 성능적인 문제
app.use('/', express.static(path.join(__dirname, 'public')))
로그인 한 유저에게만 static 라우터를 실행시키고 싶다면??
app.use('/', (req,res,next) => {
if( req.session.id ) {
express.static(__dirname, 'public')(req,res,next)
} else {
next()
}
})
2.
bodyparser
app.use(express.json()); //일반, req.body.XXX 가능
app.use(express.urlencoded({ extended: true })); // form -> multer로 이어짐
3.
cookieparser
app.use(cookieParser(process.env.COOKIE_SECRET));
app.get('/', (req,res,next) => {
req.cookies
req.signedCookies;
res.cookie('name', encodeURIComponent((name), {
expires: new Date(),
httpOnly:true,
path:'/',
}))
res.clearCookie('name', encodeURIComponent((name), {
httpOnly:true,
path:'/',
}))
res.sendFile(path.join(__dirname, 'index.html'))
})
4.
session (개인 저장소)
app.use(session({
resave: false,
saveUninitialized: false,
secret: process.env.COOKIE_SECRET,
cookie: {
httpOnly: true,
secure: false,
},
name: 'session-cookie', // 'connect.sid'
}));
다른 라우터로 데이터를 전달하고 싶을 때,
app.use((req, res, next) => {
req.session.data = '넘기고 싶은 데이터' // 계속 유지
req.data = '넘기고 싶은 데이터' // 1회성
})
app.get('/', (req,res,next) => {
app.get('hello')
req.session.data
req.data
~~~~
})
5.
morgan
app.use(morgan('dev')); // 개발할땐 dev, 배포할떄는 combined, 실무에선 combined
'개발 일지 > node.js (with express)' 카테고리의 다른 글
npm, -D, -g, package-lock.json, SemVer (0) | 2020.08.29 |
---|---|
rest api, header, 쿠키, 세션, https, http2 (0) | 2020.08.29 |
express - 미들웨어 기초 (0) | 2020.06.22 |
res.send() VS res.json() VS res.end() ?? (0) | 2020.05.09 |