Skip to content

noam1610/Loopback-heroku-mongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 

Repository files navigation

Loopback-heroku-mongo

In this tutorial we are going to create a server :

  • cloud : Heroku
  • database : Mongodb (mongolab)
  • server : Loopback

sumup

Loopback and its files

Create a new repo

On github, create manually a new repository :

capture d ecran 2015-06-24 a 16 08 48

Then, on your laptop create a folder :

mkdir my-new-folder

Now go into your folder :

cd my-new-folder

Once it's done, associate the repo and the folder : Use the command lines given in Github

    git init
    git commit -m "first commit"
    git remote add origin https://github.com/noam1610/tuto-server.git
    git push -u origin master

Or just :

git clone https://github.com/noam1610/tuto-server.git

Don't forget the :

npm init

This will create you a package.json
It's a very important file. Keep it ! Indeed, this file is in a way the id of the project. It stores all the dependencies needed to run the project and some data around the author, the git repository ... This package.json looks like :

{
  "name": "test-server",
  "version": "1.0.0",
  "description": "The project is generated by [LoopBack](http://loopback.io).",
  "main": "server/server.js",
  "dependencies": {
    "chai": "^3.0.0",
    "chalk": "^1.0.0",
      .
      .
      .
    "strip-json-comments": "^1.0.2",
    "yargs": "^3.10.0"
  },
  "scripts": {
    "test": "mocha"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/noam1610/tuto-server.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/noam1610/tuto-server/issues"
  },
  "homepage": "https://github.com/noam1610/tuto-server#readme"
}

Some required files :

First : In your folder and from the terminal :

yo sublime

Then

yo sublime:gulps

You will see this :

alt text

Only choose lint test changelog and release

Using Loopback

Make sure you have slc installed. To check it you can write slc in the terminal :

  • If it is installed, you will have a doc in the terminal
  • Else, the terminal will print an error message

If it is not installed :

npm install -g strongloop

Now we can use the slc loopback generator to create a folder. Some of its files are needed.

slc loopback tempFolder

In tempfolder, open the package.json

Copy the 3 sections :

  • dependencies
  • devDependencies
  • optionalDependencies
  "dependencies": {
    "compression": "^1.0.3",
    "cors": "^2.5.2",
    "errorhandler": "^1.1.1",
    "loopback": "^2.14.0",
    "loopback-boot": "^2.6.5",
    "loopback-datasource-juggler": "^2.19.0",
    "serve-favicon": "^2.0.1"
  },
  "optionalDependencies": {
    "loopback-explorer": "^1.1.0"
  },
  "devDependencies": {
    "jshint": "^2.5.6"
  },

Paste/add it in the initial package.json (located in the root directory).

Now move the folder server from tempFolder and add it to the root directory.

You can now delete tempFolder.

To make sure all dependencies are added :

npm install

Use a generator

Let's create an object which will be stored in the database : subsider

slc loopback:model

Now let's answer the questions :

alt text

We only create an object with an atribute : staname alt text

######Next Step:

slc loopback:datasource

Answer the questions alt text

Modify the code :

Go to common/models
You have two files : subsider.js and subsider.json

In the first one (subsider.js) add this :

module.exports = function(subsider) {
    subsider.sharedClass.find('create', true).shared = true;
    subsider.sharedClass.find('update', true).shared = false;
    subsider.sharedClass.find('upsert', true).shared = false; 
    sbsider.sharedClass.find('updateAttributes', false).shared = false;
    subsider.sharedClass.find('deleteById', true).shared = false;
    subsider.sharedClass.find('find', true).shared = false; 
    subsider.sharedClass.find('findById', true).shared = false; 
    subsider.sharedClass.find('count', true).shared = false; 
    subsider.sharedClass.find('exists', true).shared = false; 
    subsider.sharedClass.find('findOne', true).shared = false;
};

Integrating Heroku

First, you have to go to Heroku! and create an account.
Then you have to install the Heroku toolbelt.

Now you can login and create a new app :

$ heroku login
Enter your Heroku credentials.
Email: [email protected]
Password (typing will be hidden):
Authentication successful.
 heroku create

By default, Heroku will give your app a crazy name such as frozen-atoll-1232.herokuapp.com

If you want to name the app :

 heroku create nameOfMyApp

In your server folder, you have to create a new file called datasources.heroku.js :

'use strict';

module.exports = {
    mongo: {
        url: process.env.MONGO_URL
    }
};

Then, in the root directory create a file Procfile with no extension :

web: node server/server.js

The procfile is used by Heroku to know which language you use and to start to read the code with a specific file, here server/server.js

Mongo

The last file to change is datasources.js It should looks like this :

{
  "db": {
    "name": "db",
    "connector": "memory"
  },
  "mongo": {
    "name": "mongo",
    "connector": "mongodb"
  }
}

what's more, don't forget to check in model-config that your object is linked with your datasource :

"subsider": {
        "dataSource": "mongo",
        "public": true
    }

But before running to have to create manually a database in Mongolab

You have to create a user for this database: with dbuser and dbpassword

mongolabb

Then :

usermongo

Copy the uri given. It should look like :

mongodb://<dbuser>:<dbpassword>@ds061148.mongolab.com:61148/tuto-server

Don't forget to change dbuser and dbpassword without <>

Then in the terminal write :

heroku config:set NODE_ENV=heroku
heroku config:set MONGO_URL=mongodb://myuser:[email protected]:61148/tuto-server

Now we can commit and push:

git cm "feat(app):yo!!"
git push
git push heroku

Now let's hope it works:

open heroku

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published