Server-side NodeJS Scaffolding

1. Generate an Express Application

Install express-generator globally by typing the following at the prompt:

npm install express-generator -g

To create an Express application, type the following at the prompt:

express exampleServer

Move to the exampleServer and install all the dependencies by typing the following at the prompt:

cd exampleServer
npm install

Add a file named .gitignore and write the following to the file:

node_modules

Now the Express server is ready to start by typing the following at the prompt:

npm start

By default, the Express server is setup at http://localhost:3000/.

2. Setup MongoDB Server

Create a folder named data locally. Then type the following at the prompt:

mongod — dbpath=data

By default, the MongoDB server is setup at http://localhost:27017/.

3. Interaction with MongoDB Server

Install the following node modules:

npm install bluebird --save
npm install mongoose --save

By default, the mongoose Schema do not have currency type. If required, install the currency type by typing the following in the prompt:

npm install mongoose-currency --save

Now all the basic node_modules has been setup for a full REST API Express Server.


The following is an example of project architecture I have been using for server side development.

bin
-cert.csr
-certificate.pem
-private.key
-www
models
-model1.js
-model2.js
-user.js
node_modules
public
-images
-javascripts
-stylesheets
-aboutus.html
-index.html
routes
-cors.js
-index.js
-users.js
-Router1.js
-Router2.js
sessions
views
.gitignore
app.js
authenticate.js
config.js
package.json