From 894b21fa8ee5568e5ec347c03fee2286ac186bda Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Sat, 14 Mar 2015 13:24:13 +0100 Subject: [PATCH] Application MEAN d'exemple --- .bowerrc | 3 +++ .gitignore | 2 ++ app/models/nerd.js | 12 ++++++++++++ app/routes.js | 35 +++++++++++++++++++++++++++++++++++ bower.json | 11 +++++++++++ config/db.js | 4 ++++ package.json | 10 ++++++++++ public/js/app.js | 2 ++ public/js/appRoutes.js | 22 ++++++++++++++++++++++ public/js/controllers/MainCtrl.js | 6 ++++++ public/js/controllers/NerdCtrl.js | 6 ++++++ public/js/services/NerdService.js | 25 +++++++++++++++++++++++++ public/views/home.html | 7 +++++++ public/views/index.html | 37 +++++++++++++++++++++++++++++++++++++ public/views/nerd.html | 7 +++++++ server.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 16 files changed, 241 insertions(+), 0 deletions(-) create mode 100644 .bowerrc create mode 100644 .gitignore create mode 100644 app/models/nerd.js create mode 100644 app/routes.js create mode 100644 bower.json create mode 100644 config/db.js create mode 100644 package.json create mode 100644 public/js/app.js create mode 100644 public/js/appRoutes.js create mode 100644 public/js/controllers/MainCtrl.js create mode 100644 public/js/controllers/NerdCtrl.js create mode 100644 public/js/services/NerdService.js create mode 100644 public/views/home.html create mode 100644 public/views/index.html create mode 100644 public/views/nerd.html create mode 100644 server.js diff --git a/.bowerrc b/.bowerrc new file mode 100644 index 0000000..59a1cfd --- /dev/null +++ b/.bowerrc @@ -0,0 +1,3 @@ +{ + "directory": "public/libs" +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..72fc50d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +public/libs \ No newline at end of file diff --git a/app/models/nerd.js b/app/models/nerd.js new file mode 100644 index 0000000..5a09532 --- /dev/null +++ b/app/models/nerd.js @@ -0,0 +1,12 @@ +// app/models/nerd.js +// grab the mongoose module +var mongoose = require('mongoose'); + +// define our nerd model +// module.exports allows us to pass this to other files when it is called +module.exports = mongoose.model('Nerd', { + name: { + type: String, + default: '' + } +}); \ No newline at end of file diff --git a/app/routes.js b/app/routes.js new file mode 100644 index 0000000..fd6036c --- /dev/null +++ b/app/routes.js @@ -0,0 +1,35 @@ + // app/routes.js + + // grab the nerd model we just created + var Nerd = require('./models/nerd'); + + module.exports = function (app) { + + // server routes =========================================================== + // handle things like api calls + // authentication routes + + // sample api route + app.get('/api/nerds', function (req, res) { + // use mongoose to get all nerds in the database + Nerd.find(function (err, nerds) { + + // if there is an error retrieving, send the error. + // nothing after res.send(err) will execute + if (err) + res.send(err); + + res.json(nerds); // return all nerds in JSON format + }); + }); + + // route to handle creating goes here (app.post) + // route to handle delete goes here (app.delete) + + // frontend routes ========================================================= + // route to handle all angular requests + app.get('*', function (req, res) { + res.sendfile('./public/views/index.html'); // load our public/index.html file + }); + + }; \ No newline at end of file diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..dd5d95e --- /dev/null +++ b/bower.json @@ -0,0 +1,11 @@ +{ + "name": "ci-site", + "version": "0.0.1", + "dependencies": { + "bootstrap": "latest", + "font-awesome": "latest", + "animate.css": "latest", + "angular": "latest", + "angular-route": "latest" + } +} \ No newline at end of file diff --git a/config/db.js b/config/db.js new file mode 100644 index 0000000..921d49b --- /dev/null +++ b/config/db.js @@ -0,0 +1,4 @@ +// config/db.js +module.exports = { + url: 'mongodb://localhost/' +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..2c07849 --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name": "ci-site", + "main": "server.js", + "dependencies": { + "body-parser": "^1.12.0", + "express": "^4.12.2", + "method-override": "^2.3.1", + "mongoose": "^3.8.25" + } +} diff --git a/public/js/app.js b/public/js/app.js new file mode 100644 index 0000000..145886e --- /dev/null +++ b/public/js/app.js @@ -0,0 +1,2 @@ +// public/js/app.js +angular.module('sampleApp', ['ngRoute', 'appRoutes', 'MainCtrl', 'NerdCtrl', 'NerdService']); \ No newline at end of file diff --git a/public/js/appRoutes.js b/public/js/appRoutes.js new file mode 100644 index 0000000..0816e66 --- /dev/null +++ b/public/js/appRoutes.js @@ -0,0 +1,22 @@ +// public/js/appRoutes.js +angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', + function ($routeProvider, $locationProvider) { + + $routeProvider + + // home page + .when('/', { + templateUrl: 'views/home.html', + controller: 'MainController' + }) + + // nerds page that will use the NerdController + .when('/nerds', { + templateUrl: 'views/nerd.html', + controller: 'NerdController' + }); + + $locationProvider.html5Mode(true); + + } +]); \ No newline at end of file diff --git a/public/js/controllers/MainCtrl.js b/public/js/controllers/MainCtrl.js new file mode 100644 index 0000000..fa569cb --- /dev/null +++ b/public/js/controllers/MainCtrl.js @@ -0,0 +1,6 @@ +// public/js/controllers/MainCtrl.js +angular.module('MainCtrl', []).controller('MainController', function ($scope) { + + $scope.tagline = 'To the moon and back!'; + +}); \ No newline at end of file diff --git a/public/js/controllers/NerdCtrl.js b/public/js/controllers/NerdCtrl.js new file mode 100644 index 0000000..4d9fe4b --- /dev/null +++ b/public/js/controllers/NerdCtrl.js @@ -0,0 +1,6 @@ +// public/js/controllers/NerdCtrl.js +angular.module('NerdCtrl', []).controller('NerdController', function ($scope) { + + $scope.tagline = 'Nothing beats a pocket protector!'; + +}); \ No newline at end of file diff --git a/public/js/services/NerdService.js b/public/js/services/NerdService.js new file mode 100644 index 0000000..9739b86 --- /dev/null +++ b/public/js/services/NerdService.js @@ -0,0 +1,25 @@ +// public/js/services/NerdService.js +angular.module('NerdService', []).factory('Nerd', ['$http', + function ($http) { + + return { + // call to get all nerds + get: function () { + return $http.get('/api/nerds'); + }, + + + // these will work when more API routes are defined on the Node side of things + // call to POST and create a new nerd + create: function (nerdData) { + return $http.post('/api/nerds', nerdData); + }, + + // call to DELETE a nerd + delete: function (id) { + return $http.delete('/api/nerds/' + id); + } + } + + } +]); \ No newline at end of file diff --git a/public/views/home.html b/public/views/home.html new file mode 100644 index 0000000..b12bc2e --- /dev/null +++ b/public/views/home.html @@ -0,0 +1,7 @@ + + +
+

Home Page 4 Life

+ +

{{ tagline }}

+
\ No newline at end of file diff --git a/public/views/index.html b/public/views/index.html new file mode 100644 index 0000000..3f39b5e --- /dev/null +++ b/public/views/index.html @@ -0,0 +1,37 @@ + + + + + + + Starter Node and Angular + + + + + + + + + + + + + + +
+ + + +
+
+ + \ No newline at end of file diff --git a/public/views/nerd.html b/public/views/nerd.html new file mode 100644 index 0000000..5df5055 --- /dev/null +++ b/public/views/nerd.html @@ -0,0 +1,7 @@ + + +
+

Nerds and Proud

+ +

{{ tagline }}

+
diff --git a/server.js b/server.js new file mode 100644 index 0000000..3613af1 --- /dev/null +++ b/server.js @@ -0,0 +1,52 @@ +// server.js + +// modules ================================================= +var express = require('express'); +var app = express(); +var bodyParser = require('body-parser'); +var methodOverride = require('method-override'); + +// configuration =========================================== + +// config files +var db = require('./config/db'); + +// set our port +var port = process.env.PORT || 8080; + +// connect to our mongoDB database +// (uncomment after you enter in your own credentials in config/db.js) +// mongoose.connect(db.url); + +// get all data/stuff of the body (POST) parameters +// parse application/json +app.use(bodyParser.json()); + +// parse application/vnd.api+json as json +app.use(bodyParser.json({ + type: 'application/vnd.api+json' +})); + +// parse application/x-www-form-urlencoded +app.use(bodyParser.urlencoded({ + extended: true +})); + +// override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT +app.use(methodOverride('X-HTTP-Method-Override')); + +// set the static files location /public/img will be /img for users +app.use(express.static(__dirname + '/public')); + +// routes ================================================== +require('./app/routes')(app); // configure our routes + +// start app =============================================== +// startup our app at http://localhost:8080 +app.listen(port); + +// shoutout to the user +console.log('Magic happens on port ' + port); + +// expose app +exports = module.exports = app; \ No newline at end of file -- libgit2 0.21.2