NodeJS에서 Router는 내장함수로 되어있습니다.
Express 5.x - API Reference
Express 5.x API Note: This is early alpha documentation that may be incomplete and is still under development. express() Creates an Express application. The express() function is a top-level function exported by the express module. const express = require(
expressjs.com
하지만 저는 실용파라 저걸 다 읽어보는 것 보단
해보면서 이해해봅니다!
사용방법
공식문서의 예시
var express = require('express');
var app = express();
// respond with "hello world" when a GET request is made to the homepage
app.get('/', function(req, res) {
res.send('hello world');
});
1. express를 불러봅니다.
2. express 함수를 사용해 app 에 넣어봅니다.
3. 그리고 get 함수를 사용하여 첫 번째는 경로 두 번째는 함수를 넣습니다.
4. 함수 안에는 요청과 응답이 있고
5. 응답으로 hello world 를 보내라고 합니다.
이해한 것을 바탕으로 구현
// app.js 파일
const express from "express";
const app = express();
const router1 from "router";
app.use('/', router1);
// router.js 파일 안에서
import express from"express";
const router1 = express.Router();
router1.get('/', (req, res)=> {
res.send('router1 page');
});
export default router1;
추가 설명
Express는 HTTP 메소드에 해당하는 라우팅 메소드를 지원하는데
get, post, delete, put 등과 같은 메서드 사용이 가능합니다.
그리고 다음에 오는 매개 변수로 첫 번째는 경로 입니다.
그리고 함수에 들어간 요청함수 및 응답함수가 있습니다.
Express 5.x - API Reference
Express 5.x API Note: This is early alpha documentation that may be incomplete and is still under development. express() Creates an Express application. The express() function is a top-level function exported by the express module. const express = require(
expressjs.com
Express 5.x - API Reference
Express 5.x API Note: This is early alpha documentation that may be incomplete and is still under development. express() Creates an Express application. The express() function is a top-level function exported by the express module. const express = require(
expressjs.com
저 같은 경우는 필요한 경우 찾아봅니다.
'nodeJS' 카테고리의 다른 글
[Node JS] Node에서 ES6 문법 사용을 위한 Babel 설치 (0) | 2020.12.17 |
---|---|
[NodeJS] Body-Parser 사용하여 클라이언트와 서버 통신 및 DB 저장 (0) | 2020.05.14 |
[NodeJs] 몽고 DB 연결을 위한 몽구스(Mongoose) 설치 및 이용 (0) | 2020.05.13 |
[NodeJS] 백엔드 NodeJS 초기 셋팅은 이렇게 해보자! (0) | 2020.05.11 |
[NodeJs] Express 사용하여 구축하기 -01 (0) | 2019.12.04 |