수복의 프로그래밍 여행기

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

저 같은 경우는 필요한 경우 찾아봅니다.

 

 

반응형
profile

수복의 프로그래밍 여행기

@컴수복

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!