Skip to content

Commit

Permalink
example api
Browse files Browse the repository at this point in the history
  • Loading branch information
plusk01 committed Aug 28, 2015
1 parent 7cf34cd commit bcca314
Show file tree
Hide file tree
Showing 25 changed files with 155 additions and 707 deletions.
148 changes: 24 additions & 124 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,124 +1,24 @@
MEAN Skeleton
=============

This project is meant to be a reference to myself and others on how to structure a good MEAN application. This example uses pure JS as the frontend (so, not truthfully MEAN) and Jade as the template engine. This two things could easily be interchanged with other technologies.

The main focus of this example is the Node/Express side of things.

This example uses a very simple basic auth to show how to integrate different components.

## Explanation of Files/Folders ##

Click on links for more information about that part of the structure.

+ **app** - This is where your backend Node app lives.
+ [api](app/api)
+ [middleware](app/middleware)
+ [models](app/models)
+ [routes](app/routes)
+ [views](app/views)
+ **[config](config)**
+ **[public](public)**
+ css - All of your frontend css files
+ js - All of your frontend js files
+ *.bowerrc* - This is where you tell `bower` to install dependencies in a specific place (`public/lib`)
+ *.gitignore* - Make sure that you have all packages handled by `npm` and `bower` included in here so you don't source control them. View this file as an example.
+ *bower.json* - Used to keep track of which dependencies you've installed. This allows you to share this project with someone else so they can just clone and type `bower install` and the project will be setup like yours.
+ *package.json* - Similar to `bower.json`, except backend node modules are kept track here.

## A Note on Package Managers ##

Package Managers are an important part of web development. The following gives a brief explanation of `bower` and `npm`.

#### Bower ####

`bower` is used to install frontend dependencies. Always use the `--save` option so that the dependency is saved in `bower.json` for others to see.

Never source control the packages installed by `bower`, instead, source control the `bower.json` which holds all the installs. These dependencies can automagically be installed by running `bower install` in the same directory as the `bower.json`

Some common frontend packages:

```bash
bower install angular --save
bower install bootstrap --save
bower install jquery --save
bower install sweetalert --save
```

#### npm ####

`npm` is used to install backend, node dependencies. Always use the `--save` option so that the dependency is saved in `package.json` for others to see.

Never source control the packages installed by `npm`, instead, source control the `package.json` which holds all the installs. These dependencies can automagically be installed by running `npm install` in the same directory as the `package.json`

Some common node modules:

```bash
npm install express --save
npm install mongoose --save
npm install mongoose --save
```

## Steps to Setup a MEAN Project ##

These steps assume some familiarity with the terminal and that you have `npm` and `bower` installed globally.

1. Create a directory to house your project

```bash
mkdir my-mean
cd my-mean
```

2. Initialize your project with `npm init`. This will ask you a series of questions, don't worry too much about them. A `package.json` will be created.
3. Initialize your project's `bower.json` with `bower init`. Similar questions will be asked, that you can similarly not worry too much about. When asked about module types, choose `globals` only (use <kbd>space</kbd> to select).

4. Create a `.bowerrc` file:

```bash
touch .bowerrc
```

Then use `vim` or sublime to add the following to the file:

```javascript
{
"directory" : "public/lib"
}
```

This tells bower where to install dependencies that you add.

5. Create the directory structure of this repo:

```bash
mkdir -p app/{api,middleware,models,routes,socket,views} config public/{css,js}
```

6. Create or copy your `app.js`

7. Setup your `config/database.js`

8. Make sure `mongod` server is running

9. Run your app with `nodemon app.js`

*If you don't have `nodemon`, you can use `node app.js` but you will have to restart the server manually everytime you make a backend JavaScript change. Instead, install `nodemon` with `npm install nodemon --global`*
10. Visit the url:port listed in your browser!
------------------------
## API/User Authentication ##
[here](https://scotch.io/tutorials/authenticate-a-node-js-api-with-json-web-tokens)
[bcrypt](http://codahale.com/how-to-safely-store-a-password/)
[password hashing](http://devsmash.com/blog/password-authentication-with-mongoose-and-bcrypt)
------------------------
## Node Environment Setup ##
See [this](npm-setup.md) explanation.
API Demo
========

This is a simple demo of how to interact with an API.

The following routes are available:

| Method | Route | Data Expected | Response |
|--------|--------------------|---------------|-----------------|
| GET | `/api/animals` | None | List of animals |
| GET | `/api/animals/*id*`| None | Specific animal |
| POST | `/api/animals` | JSON animal | success status |
| PUT | `/api/animals/*id*`| JSON animal | success status |
| DELETE | `/api/animals/*id*`| None | success status |

JSON animal form:

```json
{
"type": "human",
"name": "Parker",
"age": 23
}
```
35 changes: 2 additions & 33 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@
// ----------------------------------------------------------------------------
var express = require('express'); // Easy API routing
var app = express(); // Create the app
var mongoose = require('mongoose'); // DB Engine for Mongo
var socketio = require('socket.io'); // socketio for real time communication
var bodyParser = require('body-parser'); // Parses POST JSON automagically
var morgan = require('morgan'); // Logging for dev
var path = require('path'); // filesystem goodies

var api = require('./app/api'); // API routes
var routes = require('./app/routes') // HTML public routes
var socket = require('./app/socket'); // socket handler
var database = require('./config/database'); // database configs

var port = process.env.PORT || 8080; // If no env var set, DEV mode

Expand All @@ -31,45 +26,19 @@ var port = process.env.PORT || 8080; // If no env var set, DEV mo

global.__base = __dirname + '/'; // so child modules can access root

mongoose.connect(database.url);
mongoose.connection.once('open', function() { console.log('DB Connected!'); });

app.set('view engine', 'jade'); // Tell Node to use Jade
app.set('views', __dirname + '/app/views'); // Tell Node where the templates are

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(morgan('dev')); // For request logging

// ----------------------------------------------------------------------------
// Custom Middleware
// ----------------------------------------------------------------------------

app.use(require('./app/middleware/basic-auth')()); // Basic auth

// ----------------------------------------------------------------------------
// Routes
// ----------------------------------------------------------------------------

app.use(express.static(path.join(__dirname, 'public'))); // for the HTML5/JS app
app.use('/uploads', express.static(__dirname + '/uploads')); // for files/images

app.use('/api', api); // all API requests will be http://host.com/api/...

/* Routes */
app.get('/', routes.index);
app.get('/admin', routes.admin) // An admin page that you want to protect
app.get('/day/:day', routes.dayAlbum)

// ----------------------------------------------------------------------------
// Listen (start app: `node app.js`)
// ----------------------------------------------------------------------------

var io = socketio.listen(app.listen(port));
console.log('Server started on port ' + port);

// ----------------------------------------------------------------------------
// Socket.io Event Handler
// ----------------------------------------------------------------------------

socket.handle(io);
app.listen(port);
console.log('Server started on port ' + port);
22 changes: 0 additions & 22 deletions app/api/README.md

This file was deleted.

Loading

0 comments on commit bcca314

Please sign in to comment.