Skip to content

Commit b45cbcd

Browse files
committed
node express jwt passport angular
1 parent 2e3cb36 commit b45cbcd

88 files changed

Lines changed: 19879 additions & 8 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

part4/logging.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ export class AppComponent {
9999

100100
필요에 의해서 만든 모듈을 공개한 개발자분들이 있습니다.
101101

102-
#### 공식사이트
102+
### 공식사이트
103103

104104
https://github.com/dbfannin/ngx-logger
105105

106-
#### 설치
106+
### 설치
107107

108108
```
109109
npm i ngx-logger
110110
```
111111

112-
#### 로깅 레벨
112+
### 로깅 레벨
113113

114114
https://github.com/dbfannin/ngx-logger/blob/master/src/lib/types/logger-level.enum.ts
115115

@@ -128,7 +128,7 @@ export enum NgxLoggerLevel {
128128
}
129129
```
130130

131-
#### 적용
131+
### 적용
132132

133133
**src\app\app.module.ts**
134134

@@ -235,7 +235,7 @@ export class AppComponent {
235235
}
236236
```
237237

238-
#### 이슈
238+
### 이슈
239239

240240
개발자 사이트를 보니 약간의 이슈가 있습니다.
241241

work/node/my-node-sns/README.md

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
# Node Express SNS Project
22

3-
pug 대신 hbs 사용
3+
pug 대신 hbs를 사용합니다. 개인취향입니다.
44

55
**참고**
66

77
* Node.js 교과서 조현영 길벗
88

9+
책 좋습니다. 많이 구매하셔서 저자를 응원합시다!
10+
11+
12+
13+
14+
15+
-----------------------------------------------------------------------
16+
17+
18+
19+
20+
921
# 새 프로젝트
1022

1123
```bash
@@ -23,6 +35,8 @@ $ npm i -g nodemon
2335
$ npm i --save-dev nodemon
2436
```
2537

38+
39+
2640
## 1. Dotenv
2741

2842
보안상 중요한 정보를 별도의 파일(.env)로 분리하고 이를 process.env 환경변수에 등록해 주는 기술입니다.
@@ -37,6 +51,8 @@ $ npm i dotenv
3751
COOKIE_SECRET=secret seed
3852
```
3953

54+
55+
4056
**app.js**
4157

4258
```js
@@ -47,14 +63,18 @@ require('dotenv').config();
4763
app.use(cookieParser(process.env.COOKIE_SECRET));
4864
```
4965

66+
67+
5068
## 2. Session
5169

52-
서버에 접속한 사용자 정보를 관리하는 세션 객체를 사용할 수 있게 해주는 기술입니다. 세션은 쿠키를 기반으로 작동하므로 쿠키 처리를 위한 미들웨어가 필요합니다. 이는 cookie-parser 패키지가 처리합니다. cookie-parser는 프로젝트 제너레이트 시 자동으로 설정됩니다. 세션 설정은 쿠키 설정 뒤에 합니다.
70+
서버에 접속한 사용자 정보를 관리하는 세션 객체를 사용할 수 있게 해주는 기술입니다. 세션은 쿠키를 기반으로 작동하므로 쿠키 처리를 위한 미들웨어가 필요합니다. 이는 cookie-parser 패키지가 처리합니다. cookie-parser는 프로젝트 제너레이트 시 자동으로 설정됩니다. 세션 설정은 쿠키 설정 뒤에 해야 합니다.
5371

5472
```bash
5573
$ npm i express-session
5674
```
5775

76+
77+
5878
**app.js**
5979

6080
```js
@@ -71,16 +91,22 @@ app.use(session({
7191
}));
7292
```
7393

94+
95+
7496
## 3. Flash Message
7597

76-
1회성 메시지를 사용자에게 전달할 필요가 있을 때 사용하는 기술입니다. POST-REDIRECT-GET 패턴 시 필요한 기술입니다.
98+
1회성 메시지를 사용자에게 전달할 필요가 있을 때 사용하는 기술입니다. POST-REDIRECT-GET 패턴을 적용하기 위해서 필요한 기술입니다.
99+
100+
77101

78102
**connect-flash**
79103

80104
```bash
81105
$ npm i connect-flash
82106
```
83107

108+
109+
84110
**app.js**
85111

86112
```js
@@ -89,6 +115,8 @@ const flash = require('connect-flash');
89115
app.use(flash());
90116
```
91117

118+
119+
92120
## 4. Sequelize
93121

94122
자바스크립트에서 이용할 수 있는 ORM 기술입니다.
@@ -106,6 +134,8 @@ Successfully created migrations folder at "C:\...\my-node-sns\migrations".
106134
Successfully created seeders folder at "C:\...\my-node-sns\seeders".
107135
```
108136

137+
138+
109139
### 데이터베이스 연결
110140

111141
**app.js**
@@ -127,6 +157,8 @@ if (app.get('env') === 'development') {
127157
}
128158
```
129159

160+
161+
130162
**config\config.json**
131163

132164
```json
@@ -161,6 +193,8 @@ if (app.get('env') === 'development') {
161193
$ sequelize db:create
162194
```
163195

196+
197+
164198
### Entity 설계
165199

166200
**models\user.js**
@@ -210,6 +244,8 @@ module.exports = (sequelize, DataTypes) => {
210244
// ) ENGINE=InnoDB;
211245
```
212246

247+
248+
213249
**models\post.js**
214250

215251
```js
@@ -242,6 +278,8 @@ module.exports = (sequelize, DataTypes) => {
242278
// ) ENGINE=InnoDB;
243279
```
244280

281+
282+
245283
**models\hashtag.js**
246284

247285
```js
@@ -268,6 +306,8 @@ module.exports = (sequelize, DataTypes) => {
268306
// ) ENGINE=InnoDB;
269307
```
270308

309+
310+
271311
**models\index.js**
272312

273313
```js
@@ -359,6 +399,8 @@ PostHashtag는 조인 테이블로써 다음 N:M 관계를 해소합니다.
359399
* Post는 다수의 Hashtag를 가진다.
360400
* Hashtag는 다수의 Post에 사용될 수 있다.
361401

402+
403+
362404
## 5. Passport
363405

364406
사용자 인증을 처리하는 기술입니다. 다양한 프로바이더와 인증을 연동하는 작업 시 매우 편리합니다. bcrypt는 패스워드를 암호화할 때 사용하는 기술입니다.
@@ -367,6 +409,8 @@ PostHashtag는 조인 테이블로써 다음 N:M 관계를 해소합니다.
367409
$ npm i passport passport-local passport-kakao bcrypt
368410
```
369411

412+
413+
370414
### 패스포트의 작동원리
371415

372416
#### Step 1. 환경설정
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Node + CSRF
2+
3+
CSRF 공격을 막기 위해서 서버에서 토큰을 클라이언트로 발행한다. 클라이언트에서 글 작성 후 데이터 전송 시 서버가 발행한 토큰을 같이 신고한다. 서버에서 생성한 토큰과 맞는지 값을 비교한 후에 토큰이 일치하는 경우에만 글을 작성하도록 구현한다.
4+
5+
## 새 프로젝트
6+
7+
```
8+
express my-node-csrf --view=hbs --css=sass
9+
```
10+
11+
## CSRF 모듈 설치
12+
13+
https://www.npmjs.com/package/csurf
14+
15+
```
16+
npm i csurf
17+
```
18+
19+
## 클라이언트에서 서버로 서버가 발행한 토큰을 신고하는 방법
20+
21+
The default value is a function that reads the token from the following locations, in order:
22+
23+
1. req.body._csrf - typically generated by the body-parser module.
24+
2. req.query._csrf - a built-in from Express.js to read from the URL query string.
25+
3. req.headers['csrf-token'] - the CSRF-Token HTTP request header.
26+
4. req.headers['xsrf-token'] - the XSRF-Token HTTP request header.
27+
5. req.headers['x-csrf-token'] - the X-CSRF-Token HTTP request header.
28+
6. req.headers['x-xsrf-token'] - the X-XSRF-Token HTTP request header

work/practice/my-node-csrf/app.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const createError = require('http-errors');
2+
const express = require('express');
3+
const app = express();
4+
const path = require('path');
5+
const cookieParser = require('cookie-parser');
6+
const logger = require('morgan');
7+
const sassMiddleware = require('node-sass-middleware');
8+
9+
10+
11+
/**
12+
* view engine setup
13+
*/
14+
15+
app.set('views', path.join(__dirname, 'views'));
16+
app.set('view engine', 'hbs');
17+
18+
19+
20+
/**
21+
* Middleware
22+
*/
23+
24+
// 1: 접속 로깅
25+
app.use(logger('dev'));
26+
27+
// 2: 정적리소스 제공
28+
app.use(express.static(path.join(__dirname, 'public')));
29+
30+
// 3: .scss ==프리 컴파일==> .css 파일 전송
31+
app.use(sassMiddleware({
32+
src: path.join(__dirname, 'public'),
33+
dest: path.join(__dirname, 'public'),
34+
indentedSyntax: false, // true = .sass and false = .scss
35+
sourceMap: false
36+
}));
37+
38+
// 4: JSON 파싱, HTML Body 파싱
39+
app.use(express.json());
40+
app.use(express.urlencoded({ extended: false }));
41+
42+
// 5: 쿠키 파싱
43+
app.use(cookieParser());
44+
// CSRF 미들웨어로 설정하면 모든 URL에 대해서 동작한다.
45+
// app.use(csrf({ cookie: true }))
46+
47+
48+
49+
/**
50+
* Routing
51+
*/
52+
53+
app.use('/', require('./routes/index'));
54+
app.use('/users', require('./routes/users'));
55+
app.use('/example', require('./routes/example'));
56+
57+
58+
59+
/**
60+
* catch 404 and forward to error handler
61+
*/
62+
63+
app.use(function(req, res, next) {
64+
next(createError(404));
65+
});
66+
67+
68+
69+
/**
70+
* error handler
71+
*/
72+
73+
app.use(function(err, req, res, next) {
74+
// set locals, only providing error in development
75+
res.locals.message = err.message;
76+
res.locals.error = req.app.get('env') === 'development' ? err : {};
77+
78+
// render the error page
79+
res.status(err.status || 500);
80+
res.render('error');
81+
});
82+
83+
84+
85+
module.exports = app;

0 commit comments

Comments
 (0)