
I also required the file that connects to the MongoDB server. The “app” variable activates express as a function, and “port” specifies the port from which the app runs - in this case port 3000. I must first require Express, as well as the model I just created.

Lastly, I exported the Pet model, so it can be used elsewhere in the app.Īfter I created my Pet model, I created an index.js file, from which I run the app itself. It also helps “sanitize,” meaning it ensures all input is tailored to a certain aesthetic, for example, everything in lowercase or - as shown above - all extra white space is trimmed from the input for decluttering purposes. It has a ton of capabilities for verifying email, passwords, postal codes, and hex codes, among many other potentially validated items. I also included the NPM package validator.

This means I throw an error and a 400 status if one is missing, though this is something done in Express. I indicated the type of values for name and animal - they are strings. What is outlined above are specifications for how I want each individual Pet entry to end up on the MongoDB server. To get started with Mongoose, I installed it as an NPM package then required it in this file. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box.

Mongoose provides a straight-forward, schema-based solution to model your application data.
