How We Coding

< app.ts >


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import * as express from 'express';
import * as bodyParser from 'body-parser';
import { testRoutes } from './apis/test/route/test.route';
 
export class Server {
    /** app 에 대한 타입 설정 */
    public app: express.Application;
    constructor() {
        /** express 설정을 위한 express 선언 */
        this.app = express();
        /** bodyParser 선언 */
        this.app.use(bodyParser.urlencoded({extended: false}));
        /** 라우터 */
        this.app.user(testRoutes.testRouter);
    }
}
cs


$ npm install body-parser --save


- 모듈 가져오기

>> import * as express from 'express'; 를 통해 모듈을 불러올 수 있다



< body-parser >

- npm 모듈 중 request body 를 json 형식으로 변환해 주는 모듈

- express 를 통해 만든 app 객체의 .use() 를 활용하여 bodyParser.urlencoded() 를 해준다.

- extended 는 아래 설명대로... (https://stackoverflow.com/questions/29960764/what-does-extended-mean-in-express-4-0)


  If extended is false, you can not post "nested object"  

  person[name] = 'caw'

  // Nested Object = { person: { name: cw } }

  If extended is true, you can do whatever way that you like. 



참고 2 : http://sjh836.tistory.com/154



< server.ts >


1
2
3
4
5
6
7
8
9
10
11
import { Server } from './app';
import * as express from 'express';
 
const port: number = 80;
const app: express.Application = new Server().app;
app.set('port', port);
app.listen(app.get('port'), () => {
    console.log('server listening on port ' + port);
}).on('error', err => {
    console.error(err);
});
cs


컴파일 후

$ sudo node server 를 통해 서버를 실행시킬 수 있다.

'H6 > backend (Node.js, tsc)' 카테고리의 다른 글

<6> tsconfig.json 을 통한 컴파일 옵션 설정  (0) 2018.01.31
<5> node-mysql (Typescript)  (0) 2018.01.22
<4> MySQL & node-mysql  (0) 2018.01.22
<3> 에러처리..  (0) 2018.01.22
<1> Node.js, Express, Typescript 환경설정  (0) 2018.01.21