- Install dependencies by running
npm install
in the project folder. - Start a Mongodb server. The project includes the shortcut
npm run-script db
. - Start the server by running
npm start
.
- tests: Independent test suite for the REST API. It can be run by opening a terminal in that folder, installing the project with
npm install
and then running the tests withnpm start
. Once the project is installed you can run the tests directly from root folder withmake test
. - node_modules: Any dependency automatically resolved with
npm install
will be placed here. Please, do not add any file here, this folder should be modified just bynpm
. This folder should be excluded from the Git repository as it can be create automatically by runningnpm install
. - 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 install
and then run it withnpm 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.js
will load automatically any other*.js
file in the folder and initialize a class for that model. - controller: This folder contains the declaration of the controller. File
controller.js
will 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.
- Check out Mongoose documentation.
- Create a file
myclass.js
in foldermodels
. That file will be automatically loaded so you will able to instantiate an object of your newly created class inapp.js
with just a line like this:var my_instance = new Myclass();
- Check out Mongoose documentation for more information about the structure of the model.
module.exports
will be passed directly tonew Schema()
to create the class. - If you need to validate a String using a regular expression take a look at
vendor/regex.js
as you may find there a regular expression to solve you problem. If there is no such regular expression, add it!
- Check out Express documentation.
- Create a file
mycontroller
incontrollers
folder 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 )
, wherereq
is the user's request andres
is the server's response. You won't usereturn
here, but will useres.send();
. - Choose a descriptive name for you method and make it available by adding it to
module.exports
. - Modify
app.js
so when the proper request arrives is your new method the one called, using a line like this:app.get( '/user', controller.User.allUsers );
.