Separating and exporting

PussinSocks·2022년 7월 19일
0

CloneCoding

목록 보기
8/20

Why and how to separate?

If the request, router, and controller stored in a single page, it's gonna be very inefficient for editing or finding the specific part of the code. So we separate the server <--> routers <--> controllers.

Since NodeJS consider a file as an independent module, when we saprate the code, we have to export and import the files.


How to export?

export default

// file to export
const globalRouter = express.router();

export default globalRouter;
// file to import
import anyName from "../path/globalRouter";

export default KNOWS the file, so the name of the imported object doesn't have to be identical. However, this can only export ONE file.

export

// more than one objects to export
export const nameOne = (req, res) => {};
export const nameTwo = (req, res) => {};
export const nameThree = (req, res) => {};
  • writing export in front of the variable can solve the problem.
import {nameOne, nameTwo, nameThree} from "../path/nameController";

exported variables should be in the curly brackets, and the name have to be EXACT SAME.

profile
On a journey to be a front-end developer

0개의 댓글