├── .editorconfig ├── .gitignore ├── .jshintignore ├── .jshintrc ├── .npmignore ├── Dockerfile ├── README.md ├── client └── README.md ├── common └── models │ └── item.json ├── fig.yml ├── package.json └── server ├── boot ├── authentication.js ├── explorer.js ├── rest-api.js └── root.js ├── config.json ├── datasources.fig.json ├── datasources.json ├── model-config.json └── server.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # http://editorconfig.org 4 | 5 | root = true 6 | 7 | [*] 8 | indent_style = space 9 | indent_size = 2 10 | end_of_line = lf 11 | charset = utf-8 12 | trim_trailing_whitespace = true 13 | insert_final_newline = true 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /.idea -------------------------------------------------------------------------------- /.jshintignore: -------------------------------------------------------------------------------- 1 | /client/ 2 | /node_modules/ 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "camelcase": true, 6 | "eqeqeq": true, 7 | "eqnull": true, 8 | "immed": true, 9 | "indent": 2, 10 | "latedef": "nofunc", 11 | "newcap": true, 12 | "nonew": true, 13 | "noarg": true, 14 | "quotmark": "single", 15 | "regexp": true, 16 | "undef": true, 17 | "unused": true, 18 | "trailing": true, 19 | "sub": true, 20 | "maxlen": 80 21 | } 22 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .project 3 | *.sublime-* 4 | .DS_Store 5 | *.seed 6 | *.log 7 | *.csv 8 | *.dat 9 | *.out 10 | *.pid 11 | *.swp 12 | *.swo 13 | node_modules 14 | coverage 15 | *.tgz 16 | *.xml 17 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:0.10 2 | 3 | ADD package.json /code/ 4 | WORKDIR /code 5 | RUN npm install 6 | ADD . /code 7 | 8 | CMD ["./node_modules/strongloop/bin/slc", "run"] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple REST API 2 | This is an API built using [StrongLoop](http://strongloop.com/). It relies on NodeJS and MongoDB. 3 | 4 | ## Prerequisites 5 | You will need: 6 | * [Git](http://git-scm.com/) 7 | * [NodeJS and NPM](https://gist.github.com/isaacs/579814) 8 | * [MongoDB](http://docs.mongodb.org/manual/installation/) 9 | * StrongLoop CLI `npm install -g strongloop` 10 | 11 | ## Getting Started 12 | 1. Make sure you have git installed 13 | 2. Install NodeJS and NPM via the provided link (we recommend using the first script). 14 | 3. Install MongoDB using the provided link. *Make sure to follow the instructions all the way down the page to AND including* "Start (Run) MongodDB". *MongoDB must be running in order for the API to run!* 15 | 4. If you have not already done so, run `npm install -g strongloop`. 16 | 5. Now run the following commands: 17 | 18 | ```bash 19 | git clone git@github.com:simpulton/simple-rest-api.git 20 | cd simple-rest-api 21 | npm install 22 | slc run 23 | ``` 24 | * Note: If you want to use the version without authentication, checkout the "without-auth" branch and restart the API if necessary. Be sure to do the same in the simple-rest-website repo. 25 | 26 | ### Alternative - Docker 27 | An alternative way to run the simple-rest-server is via [Docker](https://www.docker.com/) and [fig](http://www.fig.sh/). 28 | 29 | ```bash 30 | git clone git@github.com:simpulton/simple-rest-api.git 31 | cd simple-rest-api 32 | fig up 33 | ``` 34 | 35 | Navigate to [localhost:1337/explorer](http://localhost:1337/explorer) (or [http://192.168.59.103:1337/explorer/](http://192.168.59.103:1337/explorer/) if using boot2docker) to view your api. 36 | 37 | Congratulations! 38 | 39 | You can now play around with it using the explorer or head over to [Simple REST Website](https://github.com/simpulton/simple-rest-website) for a super-simple front-end application that connects to your new API! 40 | -------------------------------------------------------------------------------- /client/README.md: -------------------------------------------------------------------------------- 1 | ## Client 2 | 3 | This is the place for your application front-end files. 4 | -------------------------------------------------------------------------------- /common/models/item.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "item", 3 | "plural": "items", 4 | "base": "PersistedModel", 5 | "properties": { 6 | "name": { 7 | "type": "string", 8 | "required": true 9 | }, 10 | "description": { 11 | "type": "string" 12 | } 13 | }, 14 | "validations": [], 15 | "relations": {}, 16 | "acls": [ 17 | { 18 | "accessType": "*", 19 | "principalType": "ROLE", 20 | "principalId": "$everyone", 21 | "permission": "DENY" 22 | }, 23 | { 24 | "accessType": "*", 25 | "principalType": "ROLE", 26 | "principalId": "$authenticated", 27 | "permission": "ALLOW" 28 | } 29 | ], 30 | "methods": [] 31 | } 32 | -------------------------------------------------------------------------------- /fig.yml: -------------------------------------------------------------------------------- 1 | web: 2 | build: . 3 | ports: 4 | - "1337:1337" 5 | links: 6 | - mongo 7 | environment: 8 | NODE_ENV: fig 9 | 10 | mongo: 11 | image: mongo:2.6 12 | ports: 13 | - "27017:27017" 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "strongloop-api", 3 | "version": "0.0.0", 4 | "main": "server/server.js", 5 | "scripts": { 6 | "pretest": "jshint ." 7 | }, 8 | "dependencies": { 9 | "compression": "^1.0.3", 10 | "errorhandler": "^1.1.1", 11 | "strongloop": "^2.10.3", 12 | "loopback": "^2.0.0", 13 | "loopback-boot": "^2.0.0", 14 | "loopback-connector-mongodb": "^1.4.4", 15 | "loopback-datasource-juggler": "^2.0.0", 16 | "serve-favicon": "^2.0.1" 17 | }, 18 | "optionalDependencies": { 19 | "loopback-explorer": "^1.1.0" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /server/boot/authentication.js: -------------------------------------------------------------------------------- 1 | module.exports = function enableAuthentication(server) { 2 | // enable authentication 3 | server.enableAuth(); 4 | }; 5 | -------------------------------------------------------------------------------- /server/boot/explorer.js: -------------------------------------------------------------------------------- 1 | module.exports = function mountLoopBackExplorer(server) { 2 | var explorer; 3 | try { 4 | explorer = require('loopback-explorer'); 5 | } catch(err) { 6 | console.log( 7 | 'Run `npm install loopback-explorer` to enable the LoopBack explorer' 8 | ); 9 | return; 10 | } 11 | 12 | var restApiRoot = server.get('restApiRoot'); 13 | 14 | var explorerApp = explorer(server, { basePath: restApiRoot }); 15 | server.use('/explorer', explorerApp); 16 | server.once('started', function() { 17 | var baseUrl = server.get('url').replace(/\/$/, ''); 18 | // express 4.x (loopback 2.x) uses `mountpath` 19 | // express 3.x (loopback 1.x) uses `route` 20 | var explorerPath = explorerApp.mountpath || explorerApp.route; 21 | console.log('Browse your REST API at %s%s', baseUrl, explorerPath); 22 | }); 23 | }; 24 | -------------------------------------------------------------------------------- /server/boot/rest-api.js: -------------------------------------------------------------------------------- 1 | module.exports = function mountRestApi(server) { 2 | var restApiRoot = server.get('restApiRoot'); 3 | server.use(restApiRoot, server.loopback.rest()); 4 | }; 5 | -------------------------------------------------------------------------------- /server/boot/root.js: -------------------------------------------------------------------------------- 1 | module.exports = function(server) { 2 | // Install a `/` route that returns server status 3 | var router = server.loopback.Router(); 4 | router.get('/', server.loopback.status()); 5 | server.use(router); 6 | }; 7 | -------------------------------------------------------------------------------- /server/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "restApiRoot": "/api", 3 | "host": "0.0.0.0", 4 | "port": 1337, 5 | "url": "http://localhost:1337/" 6 | } 7 | -------------------------------------------------------------------------------- /server/datasources.fig.json: -------------------------------------------------------------------------------- 1 | { 2 | "mongoDB": { 3 | "host": "mongo", 4 | "port": 27017, 5 | "database": "strongloop", 6 | "name": "mongoDB", 7 | "connector": "mongodb" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /server/datasources.json: -------------------------------------------------------------------------------- 1 | { 2 | "db": { 3 | "name": "db", 4 | "connector": "memory" 5 | }, 6 | "mongoDB": { 7 | "host": "localhost", 8 | "port": 27017, 9 | "database": "strongloop", 10 | "name": "mongoDB", 11 | "connector": "mongodb" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /server/model-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "_meta": { 3 | "sources": [ 4 | "../common/models", 5 | "./models" 6 | ] 7 | }, 8 | "User": { 9 | "dataSource": "mongoDB" 10 | }, 11 | "AccessToken": { 12 | "dataSource": "db", 13 | "public": false 14 | }, 15 | "ACL": { 16 | "dataSource": "db", 17 | "public": false 18 | }, 19 | "RoleMapping": { 20 | "dataSource": "db", 21 | "public": false 22 | }, 23 | "Role": { 24 | "dataSource": "db", 25 | "public": false 26 | }, 27 | "item": { 28 | "dataSource": "mongoDB", 29 | "public": true 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /server/server.js: -------------------------------------------------------------------------------- 1 | var loopback = require('loopback'); 2 | var boot = require('loopback-boot'); 3 | 4 | var app = module.exports = loopback(); 5 | 6 | // Set up the /favicon.ico 7 | app.use(loopback.favicon()); 8 | 9 | // request pre-processing middleware 10 | app.use(loopback.compress()); 11 | 12 | // -- Add your pre-processing middleware here -- 13 | 14 | // boot scripts mount components like REST API 15 | boot(app, __dirname); 16 | 17 | // -- Mount static files here-- 18 | // All static middleware should be registered at the end, as all requests 19 | // passing the static middleware are hitting the file system 20 | // Example: 21 | // app.use(loopback.static(path.resolve(__dirname', '../client'))); 22 | 23 | // Requests that get this far won't be handled 24 | // by any middleware. Convert them into a 404 error 25 | // that will be handled later down the chain. 26 | app.use(loopback.urlNotFound()); 27 | 28 | // The ultimate error handler. 29 | app.use(loopback.errorHandler()); 30 | 31 | app.start = function() { 32 | // start the web server 33 | return app.listen(function() { 34 | app.emit('started'); 35 | console.log('Web server listening at: %s', app.get('url')); 36 | }); 37 | }; 38 | 39 | // start the server if `$ node server.js` 40 | if (require.main === module) { 41 | app.start(); 42 | } 43 | --------------------------------------------------------------------------------