> For the complete documentation index, see [llms.txt](https://adam-37.gitbook.io/joomadeung/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://adam-37.gitbook.io/joomadeung/node.js/node.js-express-mysql/next.md).

# next 활용법

* undefined인 asdasdad에서 error를 catch해서 그 다음 미들웨어인 `res.send('에러났습니다!!!!!!!!!!!!!!!!!!!!!!!')` 가 보여진다.

```jsx
app.use(
  (req, res, next) => {
    console.log('1 모든 요청에 실행하고 싶어요');
    next();
  },
  (req, res, next) => {
    try {
      console.log('에러 try catch');
      console.log(asdasdad);
    } catch (err) {
      console.log(err);
      next(err); // next에 인수가 들어가있으면 error처리 미들웨어로 넘어감.
    }
  }
);

// 에러처리 미들웨어
app.use((err, req, res, next) => {
  console.error(err);
  res.send('에러났습니다!!!!!!!!!!!!!!!!!!!!!!!');
});
```

* 중복을 제거할 수 있다.
  * if문 안에 true이면 다음 미들웨어인 `console.log('예, 물론 실행됩니다.')`가 실행되고
  * false라면 바로 아래 미들웨어인 `console.log('실행되나요?')` 가 실행된다. 중복을 줄이기에 용이하다.

```jsx
app.get(
  '/',
  (req, res, next) => {
    res.sendFile(path.join(__dirname, 'index.html'));
    if (true) {
      next('route'); // 예, 물론 실행됩니다.
    } else {
      next(); // 실행되나요?
    }
  },
  (req, res) => {
    console.log('실행되나요?');
  }
);

app.get('/', (req, res) => {
  console.log('예, 물론 실행됩니다.');
});
```

```jsx
app.get(
  '/',
  (req, res, next) => {
    res.sendFile(path.join(__dirname, 'index.html'));
    if (false) {
      next('route'); // 예, 물론 실행됩니다.
    } else {
      next(); // 실행되나요?
    }
  },
  (req, res) => {
    console.log('실행되나요?'); // false일 경우 이 부분이 실행됨
  }
);

app.get('/', (req, res) => {
  console.log('예, 물론 실행됩니다.');
});
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://adam-37.gitbook.io/joomadeung/node.js/node.js-express-mysql/next.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
