기존(310)/🏀Node
[Node.js]미들웨어 작동법. url
조각남자
2022. 6. 8. 19:16
|
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
|
const http = require('http'); // http 열기
const express = require('express'); // 익스프레스 열기
const { nextTick } = require('process');
const server = express(); //서버 열기
server.use('/',(res,req,next)=>{ // '/' <인덱스페이지가 아니라 / 부터 시작하기
console.log('/인덱스페이지가아니라 /부터 시작');
next(); // 여긴끝이지 밑에 ㅣ작하자
})
server.use('/add-product',(req,res,next)=>{ // localhost:4000/add-product 실행
console.log('여기로 넘어와랑');
res.send('<h1>product page</hi>')
})
server.use('/',(req,res,next)=>{ // next()함수처리가 없기에
console.log('여기로 넘어와랑');
res.send('<h1>gi</hi>')
})
server.listen(4000);
|
cs |
Middel 웨어가 작동하는 방식
8번째인 = > server.use('/',(res,req,next)=>{ 인 경우 localhost:4000/ 뒤에 아무거나 사용해도 서버가 열림
server.use('/add-product',(req,res,next)=>{ // localhost:4000/add-product 실행 '/' 것도 실행되고 /add-product 같이 실행된다.
next() 함수는 꼭 있어야 다음페이지로 넘어갈수 있다...