- Install dependencies by running
npm installin the project folder.
- Start a Mongodb server. The project includes the shortcut
npm run-script db.
- Start the server by running
- tests: Independent test suite for the REST API. It can be run by opening a terminal in that folder, installing the project with
npm installand then running the tests with
npm start. Once the project is installed you can run the tests directly from root folder with
- node_modules: Any dependency automatically resolved with
npm installwill be placed here. Please, do not add any file here, this folder should be modified just by
npm. This folder should be excluded from the Git repository as it can be create automatically by running
- docs: Independent project where is located the REST API documentation. To browse the documentation just open the directory in a terminal window, instal the project with
npm installand then run it with
npm start. The log will show you the URL where you can browse the documentation.
- model: This folder contains the declaration of the model, using Mongoose ODM. File
model.jswill load automatically any other
*.jsfile in the folder and initialize a class for that model.
- controller: This folder contains the declaration of the controller. File
controller.jswill load automatically any other `.js file in the folder and hook it to the controller module.
- vendor: Small utilities like regular expressions to verify emails or helpers to print messages in the log with fancy colors.
Adding a new model
- Check out Mongoose documentation.
- Create a file
models. That file will be automatically loaded so you will able to instantiate an object of your newly created class in
app.jswith just a line like this:
var my_instance = new Myclass();
- Check out Mongoose documentation for more information about the structure of the model.
module.exportswill be passed directly to
new Schema()to create the class.
- If you need to validate a String using a regular expression take a look at
vendor/regex.jsas you may find there a regular expression to solve you problem. If there is no such regular expression, add it!
Adding a new controller
- Check out Express documentation.
- Create a file
controllersfolder if required. The best thing to do is to create just one controller file for each model file, as one controller file can be responsible for more than just one action.
- If you have created a new file you'll need to access the model in the controller, so require the model with this line:
var model = require( '../models/model.js' );. Remember that NodeJS caches any require, so you are not executing the module but getting access to the previously processed module, that means that this is very fast!
- Add a function to handle the use case you want to implement. Remember that this function will have the following signature:
function( req, res ), where
reqis the user's request and
resis the server's response. You won't use
returnhere, but will use
- Choose a descriptive name for you method and make it available by adding it to
app.jsso when the proper request arrives is your new method the one called, using a line like this:
app.get( '/user', controller.User.allUsers );.