Commit 894b21fa8ee5568e5ec347c03fee2286ac186bda
1 parent
ceedb37d
Application MEAN d'exemple
D'après https://scotch.io/tutorials/setting-up-a-mean-stack-single-page-application
Showing
16 changed files
with
241 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,12 @@ | @@ -0,0 +1,12 @@ | ||
1 | +// app/models/nerd.js | ||
2 | +// grab the mongoose module | ||
3 | +var mongoose = require('mongoose'); | ||
4 | + | ||
5 | +// define our nerd model | ||
6 | +// module.exports allows us to pass this to other files when it is called | ||
7 | +module.exports = mongoose.model('Nerd', { | ||
8 | + name: { | ||
9 | + type: String, | ||
10 | + default: '' | ||
11 | + } | ||
12 | +}); | ||
0 | \ No newline at end of file | 13 | \ No newline at end of file |
@@ -0,0 +1,35 @@ | @@ -0,0 +1,35 @@ | ||
1 | + // app/routes.js | ||
2 | + | ||
3 | + // grab the nerd model we just created | ||
4 | + var Nerd = require('./models/nerd'); | ||
5 | + | ||
6 | + module.exports = function (app) { | ||
7 | + | ||
8 | + // server routes =========================================================== | ||
9 | + // handle things like api calls | ||
10 | + // authentication routes | ||
11 | + | ||
12 | + // sample api route | ||
13 | + app.get('/api/nerds', function (req, res) { | ||
14 | + // use mongoose to get all nerds in the database | ||
15 | + Nerd.find(function (err, nerds) { | ||
16 | + | ||
17 | + // if there is an error retrieving, send the error. | ||
18 | + // nothing after res.send(err) will execute | ||
19 | + if (err) | ||
20 | + res.send(err); | ||
21 | + | ||
22 | + res.json(nerds); // return all nerds in JSON format | ||
23 | + }); | ||
24 | + }); | ||
25 | + | ||
26 | + // route to handle creating goes here (app.post) | ||
27 | + // route to handle delete goes here (app.delete) | ||
28 | + | ||
29 | + // frontend routes ========================================================= | ||
30 | + // route to handle all angular requests | ||
31 | + app.get('*', function (req, res) { | ||
32 | + res.sendfile('./public/views/index.html'); // load our public/index.html file | ||
33 | + }); | ||
34 | + | ||
35 | + }; | ||
0 | \ No newline at end of file | 36 | \ No newline at end of file |
@@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
1 | +{ | ||
2 | + "name": "ci-site", | ||
3 | + "version": "0.0.1", | ||
4 | + "dependencies": { | ||
5 | + "bootstrap": "latest", | ||
6 | + "font-awesome": "latest", | ||
7 | + "animate.css": "latest", | ||
8 | + "angular": "latest", | ||
9 | + "angular-route": "latest" | ||
10 | + } | ||
11 | +} | ||
0 | \ No newline at end of file | 12 | \ No newline at end of file |
@@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
1 | +// public/js/appRoutes.js | ||
2 | +angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', | ||
3 | + function ($routeProvider, $locationProvider) { | ||
4 | + | ||
5 | + $routeProvider | ||
6 | + | ||
7 | + // home page | ||
8 | + .when('/', { | ||
9 | + templateUrl: 'views/home.html', | ||
10 | + controller: 'MainController' | ||
11 | + }) | ||
12 | + | ||
13 | + // nerds page that will use the NerdController | ||
14 | + .when('/nerds', { | ||
15 | + templateUrl: 'views/nerd.html', | ||
16 | + controller: 'NerdController' | ||
17 | + }); | ||
18 | + | ||
19 | + $locationProvider.html5Mode(true); | ||
20 | + | ||
21 | + } | ||
22 | +]); | ||
0 | \ No newline at end of file | 23 | \ No newline at end of file |
@@ -0,0 +1,25 @@ | @@ -0,0 +1,25 @@ | ||
1 | +// public/js/services/NerdService.js | ||
2 | +angular.module('NerdService', []).factory('Nerd', ['$http', | ||
3 | + function ($http) { | ||
4 | + | ||
5 | + return { | ||
6 | + // call to get all nerds | ||
7 | + get: function () { | ||
8 | + return $http.get('/api/nerds'); | ||
9 | + }, | ||
10 | + | ||
11 | + | ||
12 | + // these will work when more API routes are defined on the Node side of things | ||
13 | + // call to POST and create a new nerd | ||
14 | + create: function (nerdData) { | ||
15 | + return $http.post('/api/nerds', nerdData); | ||
16 | + }, | ||
17 | + | ||
18 | + // call to DELETE a nerd | ||
19 | + delete: function (id) { | ||
20 | + return $http.delete('/api/nerds/' + id); | ||
21 | + } | ||
22 | + } | ||
23 | + | ||
24 | + } | ||
25 | +]); | ||
0 | \ No newline at end of file | 26 | \ No newline at end of file |
@@ -0,0 +1,37 @@ | @@ -0,0 +1,37 @@ | ||
1 | +<!-- public/index.html --> | ||
2 | +<!doctype html> | ||
3 | +<html lang="en"> | ||
4 | + <head> | ||
5 | + <meta charset="UTF-8"> | ||
6 | + <base href="/"> | ||
7 | + <title>Starter Node and Angular</title> | ||
8 | + <!-- CSS --> | ||
9 | + <link rel="stylesheet" href="libs/bootstrap/dist/css/bootstrap.min.css"> | ||
10 | + <link rel="stylesheet" href="css/style.css"> <!-- custom styles --> | ||
11 | + <!-- JS --> | ||
12 | + <script src="libs/angular/angular.min.js"></script> | ||
13 | + <script src="libs/angular-route/angular-route.min.js"></script> | ||
14 | + <!-- ANGULAR CUSTOM --> | ||
15 | + <script src="js/controllers/MainCtrl.js"></script> | ||
16 | + <script src="js/controllers/NerdCtrl.js"></script> | ||
17 | + <script src="js/services/NerdService.js"></script> | ||
18 | + <script src="js/appRoutes.js"></script> | ||
19 | + <script src="js/app.js"></script> | ||
20 | + </head> | ||
21 | + <body ng-app="sampleApp" ng-controller="NerdController"> | ||
22 | + <div class="container"> | ||
23 | + <!-- HEADER --> | ||
24 | + <nav class="navbar navbar-inverse"> | ||
25 | + <div class="navbar-header"> | ||
26 | + <a class="navbar-brand" href="/">Stencil: Node and Angular</a> | ||
27 | + </div> | ||
28 | + <!-- LINK TO OUR PAGES. ANGULAR HANDLES THE ROUTING HERE --> | ||
29 | + <ul class="nav navbar-nav"> | ||
30 | + <li><a href="/nerds">Nerds</a></li> | ||
31 | + </ul> | ||
32 | + </nav> | ||
33 | + <!-- ANGULAR DYNAMIC CONTENT --> | ||
34 | + <div ng-view></div> | ||
35 | + </div> | ||
36 | + </body> | ||
37 | +</html> | ||
0 | \ No newline at end of file | 38 | \ No newline at end of file |
@@ -0,0 +1,52 @@ | @@ -0,0 +1,52 @@ | ||
1 | +// server.js | ||
2 | + | ||
3 | +// modules ================================================= | ||
4 | +var express = require('express'); | ||
5 | +var app = express(); | ||
6 | +var bodyParser = require('body-parser'); | ||
7 | +var methodOverride = require('method-override'); | ||
8 | + | ||
9 | +// configuration =========================================== | ||
10 | + | ||
11 | +// config files | ||
12 | +var db = require('./config/db'); | ||
13 | + | ||
14 | +// set our port | ||
15 | +var port = process.env.PORT || 8080; | ||
16 | + | ||
17 | +// connect to our mongoDB database | ||
18 | +// (uncomment after you enter in your own credentials in config/db.js) | ||
19 | +// mongoose.connect(db.url); | ||
20 | + | ||
21 | +// get all data/stuff of the body (POST) parameters | ||
22 | +// parse application/json | ||
23 | +app.use(bodyParser.json()); | ||
24 | + | ||
25 | +// parse application/vnd.api+json as json | ||
26 | +app.use(bodyParser.json({ | ||
27 | + type: 'application/vnd.api+json' | ||
28 | +})); | ||
29 | + | ||
30 | +// parse application/x-www-form-urlencoded | ||
31 | +app.use(bodyParser.urlencoded({ | ||
32 | + extended: true | ||
33 | +})); | ||
34 | + | ||
35 | +// override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT | ||
36 | +app.use(methodOverride('X-HTTP-Method-Override')); | ||
37 | + | ||
38 | +// set the static files location /public/img will be /img for users | ||
39 | +app.use(express.static(__dirname + '/public')); | ||
40 | + | ||
41 | +// routes ================================================== | ||
42 | +require('./app/routes')(app); // configure our routes | ||
43 | + | ||
44 | +// start app =============================================== | ||
45 | +// startup our app at http://localhost:8080 | ||
46 | +app.listen(port); | ||
47 | + | ||
48 | +// shoutout to the user | ||
49 | +console.log('Magic happens on port ' + port); | ||
50 | + | ||
51 | +// expose app | ||
52 | +exports = module.exports = app; | ||
0 | \ No newline at end of file | 53 | \ No newline at end of file |