We will build a movie API that allows you to add actor and movie information to a database and connect actors with movies, and vice versa. This will make use of the information introduced in Chapter 1, Building a Basic Express Site, and give you a hands-on feel for what Express.js offers. We will cover the following topics in this chapter:
Folder structure is a very controversial topic. Though there are many clean ways to structure your project, we will use the following code for the remainder of our chapters:
chapter2 ├── app.js ├── package.json ├── node_modules │└── npm package folders ├── src │├── lib │├── models │├── routes └── test
Let's take a look this at in detail:
app.js
: It is conventional to have the main app.js
file in the root directory. The app.js
is the entry point of our application and will be used to launch the server.package.json:
As with any Node.js app, we have package.json
in the root folder specifying our application name and version as well as all of our npm dependencies.node_modules:
The node_modules
folder and its content are generated via npm installation and should usually be ignored in your version control of choice because it depends on the platform the app runs on. Having said that, according to the npm FAQ, it is probably better to commit the node_modules
folder as well.src
: The src
folder contains all the logic of the application.lib
: Within the src
folder, we have the lib
folder, which contains the core of the application. This includes the middleware, routes, and creating the database connection.models
: The models
folder contains our mongoose
models, which defines the structure and logic of the models we want to manipulate and save.routes
: The routes
folder contains the code for all the endpoints the API is able to serve.test
: The test
folder will contain our functional tests using Mocha as well as two other node modules, should
and supertest
, to make it easier to aim for 100 percent coverage.