인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

hxhxhx님의 프로필 이미지
hxhxhx

작성한 질문수

[리뉴얼] Node.js 교과서 - 기본부터 프로젝트 실습까지

미들웨어 사용하기

next()후 다음에 실행할 미들웨어가 없으면

작성

·

252

0

 

app.get('/',(req,res,next)=>{
    console.log("/ 라우터");
    res.sendFile(path.join(__dirname,'/index.html'));
    next();
},(req,res,next)=>{
    console.log("/ 라우터의 두번째 미들웨어");
// next();
});


app.get('/about',(req,res)=>{
    console.log("about 라우터");
});

-> 정상

 

 


app.get('/',(req,res,next)=>{
    console.log("/ 라우터");
    res.sendFile(path.join(__dirname,'/index.html'));
    next();
},(req,res,next)=>{
    console.log("/ 라우터의 두번째 미들웨어");
    next();
});


app.get('/about',(req,res)=>{
    console.log("about 라우터");
});

-> 404 Not Found

 

app.get('/',(req,res,next)=>{
    console.log("/ 라우터");
    res.sendFile(path.join(__dirname,'/index.html'));
    next();
},(req,res,next)=>{
    console.log("/ 라우터의 두번째 미들웨어");
    next();
});

app.get('*',(req,res)=>{
    console.log("* 라우터");
});

app.get('/about',(req,res)=>{
    console.log("about 라우터");
});

-> 정상

 

세가지 코드를 비교해봤는데,

localhost:3000/ 접속시 두번째 코드에서 404 Not Found가 뜨는 이유는 next()로 다음 미들웨어를 실행했는데, 실행할 미들웨어가 없어서 404가 뜨는건가요?

답변 1

1

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

네 next했는데 다음 미들웨어가 없어서 그렇습니다.

hxhxhx님의 프로필 이미지
hxhxhx

작성한 질문수

질문하기