Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Organizing Express Code | Section
Building APIs with Express.js

bookOrganizing Express Code

メニューを表示するにはスワイプしてください

As your application grows, keeping everything in one file becomes hard to manage.

To keep your code clean, you can split it into multiple files.

For example, you can move routes into a separate file:

// routes/users.js
const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
  res.send('users list');
});

module.exports = router;

Then use it in your main file:

// app.js
const express = require('express');
const usersRoutes = require('./routes/users');

const app = express();

app.use('/users', usersRoutes);

app.listen(3000);

This approach helps separate different parts of your application and makes it easier to maintain.

You can also separate logic into different files, often called controllers, but the main idea is to avoid putting everything in one place.

question mark

Why should you split Express code into multiple files?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  16

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  16
some-alt