2022-04-30 15:00:04 +01:00
|
|
|
---
|
2024-06-15 11:00:03 +01:00
|
|
|
tags:
|
|
|
|
- node-js
|
|
|
|
- REST
|
|
|
|
- APIs
|
2022-04-30 15:00:04 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
# Structuring Express applications
|
|
|
|
|
|
|
|
In general, the following application structure works well:
|
|
|
|
|
|
|
|
```
|
|
|
|
\
|
|
|
|
routes\
|
2022-08-28 10:37:22 +01:00
|
|
|
models\
|
2022-04-30 15:00:04 +01:00
|
|
|
middlewear\
|
|
|
|
config\
|
|
|
|
public\
|
2022-08-28 10:37:22 +01:00
|
|
|
index.js
|
2022-04-30 15:00:04 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2022-08-28 10:37:22 +01:00
|
|
|
- **`routes/`**
|
2022-04-30 15:00:04 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
- This should house all our RESTful API endpoints for a given resource which
|
|
|
|
we define as a module. For instance if we are handling API requests to
|
|
|
|
`/api/courses/` we would have a module at `/routes/courses.js`
|
2022-04-30 15:00:04 +01:00
|
|
|
|
2022-08-28 10:37:22 +01:00
|
|
|
- **`models/`**
|
2024-02-02 15:58:13 +00:00
|
|
|
- Here we define our database schemas: definitions for the structure of the
|
|
|
|
data objects we want to return from the database.
|
|
|
|
- In the case of Mongo we would have a file for each of our Mongoose models
|
|
|
|
which will belong to an overall collection of which there are document
|
|
|
|
instances.
|
|
|
|
- Our model files should map onto our route modules; there should generally be
|
|
|
|
at least one model file for each module. The model stands as an abstract
|
|
|
|
representation of the data that we will be handling with our request paths
|
|
|
|
in the routes file.
|