Skip to content

Commit 9a861c1

Browse files
author
Nathan Grunzweig
committed
added express-angular-mongo example content
1 parent c6838bb commit 9a861c1

File tree

13 files changed

+552
-2
lines changed

13 files changed

+552
-2
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
1-
# express-angular-mongo
2-
codefresh labs express-angular-mongo example
1+
# Express-Angular-Mongo App
2+
3+
A express node.js app built with Angular and MongoDB. For demonstration purposes and a tutorial.
4+
5+
Node provides the RESTful API. Angular provides the frontend and accesses the API, MongoDB provide NoSQL database.
6+
7+
## Example
8+
9+
The example allow you to see how those 3 great technologies / tools works together,
10+
11+
and how easy it to build amazing apps this way.
12+
13+
### What to do?
14+
15+
Simply add new contact and see how you can deal with the basics of inserting, editing and deleting easily.
16+
17+
## App run
18+
19+
Click on the 'Play' button at the top toolbar.
20+
21+
## App view
22+
23+
View in browser at [http://localhost:9090](http://localhost:9090)
24+
25+
![Express-angular-mongo](http://i.imgur.com/DKxrGT0.png)

app.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*eslint-env node */
2+
3+
// set up ======================================================================
4+
var express = require('express');
5+
var app = express(); // create our app w/ express
6+
var port = process.env.PORT || 9090;
7+
var mongoose = require('mongoose'); // mongoose for mongodb // set the port
8+
var database = require('./config/database'); // load the database config
9+
10+
var morgan = require('morgan'); // log requests to the console (express4)
11+
var bodyParser = require('body-parser'); // pull information from HTML POST (express4)
12+
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4)
13+
14+
// configuration ===============================================================
15+
mongoose.connect(database.url); // connect to mongoDB database on modulus.io
16+
17+
app.use(express.static(__dirname + '/public')); // set static path
18+
app.use(morgan('dev')); // log every request to the console
19+
app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded
20+
app.use(bodyParser.json()); // parse application/json
21+
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
22+
app.use(methodOverride());
23+
24+
// routes
25+
require('./app/routes.js')(app);
26+
27+
28+
app.listen(port);
29+
console.log("App listening on port " + port);

app/models/contacts.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*eslint-env node */
2+
3+
var mongoose = require('mongoose');
4+
5+
module.exports = mongoose.model('Contact', {
6+
name : String,
7+
email: String,
8+
done : Boolean
9+
});

app/routes.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*eslint-env node */
2+
3+
var Contact = require('./models/contacts');
4+
5+
module.exports = function(app) {
6+
/* API */
7+
8+
// get all contacts
9+
app.get('/api/contacts', function(req, res) {
10+
11+
// mongoose get all contacts
12+
Contact.find(function(err, contacts) {
13+
14+
// send an error
15+
if (err)
16+
res.send(err)
17+
18+
res.json(contacts); // return all contacts
19+
});
20+
});
21+
22+
// get contact form data and dave it
23+
app.post('/api/contact', function(req, res) {
24+
25+
// insert new contact
26+
Contact.create({
27+
name : req.body.form_data.name,
28+
email: req.body.form_data.email,
29+
done : false
30+
}, function(err, contact) {
31+
if (err)
32+
res.send(err);
33+
34+
Contact.find(function(err, contacts) {
35+
if (err)
36+
res.send(err);
37+
38+
var congrats = "Congrats "+req.body.form_data.name+"! ";
39+
res.send({status:congrats + " Your form has been sent!"});
40+
});
41+
});
42+
43+
});
44+
45+
// contact update
46+
app.put('/api/contact:contact_id', function(req, res) {
47+
var id = req.params.contact_id;
48+
console.log("Saving contact: " + id);
49+
50+
Contact.findById(id , function(err, contact) {
51+
if (err)
52+
res.send(err);
53+
54+
// fields that can be updated:
55+
contact.name = req.body.name ? req.body.name : contact.name;
56+
contact.email = req.body.email ? req.body.email : contact.email;
57+
58+
contact.save(function(err) {
59+
if (err)
60+
res.send(err);
61+
62+
res.send({status:"ok"});
63+
});
64+
});
65+
});
66+
67+
// contact delete
68+
app.delete('/api/contact:contact_id', function(req, res) {
69+
var id = req.params.contact_id;
70+
console.log("Deleting contact: " + id);
71+
Contact.remove({
72+
_id : id
73+
}, function(err, contact) {
74+
if (err)
75+
res.send(err);
76+
77+
res.send({status:"ok"});
78+
});
79+
});
80+
81+
82+
/* APPLICATION */
83+
app.get('*', function(req, res) {
84+
// load index.html otherwise
85+
res.sendfile('./public/index.html');
86+
});
87+
};

config/database.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*eslint-env node */
2+
3+
module.exports = {
4+
// mongo database connection url
5+
url : 'mongodb://localhost/express_angular_mongo_demo'
6+
};

launchConfigurations/node.launch

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"Name": "express-angular-mongo",
3+
"ServiceId":"io.codefresh.orion.client.node.runner",
4+
"Params": {
5+
"Name" : "Run",
6+
"Target" : {
7+
"Cmd": "node start",
8+
"Module": "app.js",
9+
"Space": "Node"
10+
}
11+
},
12+
"Url":"http://localhost:9090/",
13+
"Path":"",
14+
"Type":"NodeRunner"
15+
}

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name" : "express-angular-mongo",
3+
"version" : "0.0.0",
4+
"description" : "Simple express angular and mongo application.",
5+
"main" : "app.js",
6+
"author" : "aviad@codefresh.io",
7+
"dependencies" : {
8+
"express" : "~4.7.2",
9+
"mongoose" : "~3.6.2",
10+
"morgan" : "~1.2.2",
11+
"body-parser": "~1.5.2",
12+
"method-override": "~2.1.2"
13+
}
14+
}

project.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Url":"",
3+
"Name":"express-angular-mongo",
4+
"Description":"NodeJs example includes AngularJS and Mongo DB"
5+
}

public/app.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*eslint-env browser */
2+
3+
var expressAngular = angular.module('expressAngular', []);
4+
5+
function mainController($scope, $http) {
6+
$scope.loading = false;
7+
$scope.status = "";
8+
9+
$scope.contacts = [];
10+
$scope.edit_flag = [];
11+
$scope.loadContacts = loadContacts = function() {
12+
$http.get('/api/contacts').
13+
success(function(data, status, headers, config) {
14+
$scope.contacts = data;
15+
var c = 0;
16+
for(i in data) {
17+
$scope.edit_flag[c] = false;
18+
}
19+
}).
20+
error(function(data, status, headers, config) {
21+
// do something
22+
});
23+
};
24+
25+
26+
// send form data
27+
$scope.contactSend = function() {
28+
$scope.loading = true;
29+
$http.post('/api/contact', {form_data:this.data}).
30+
success(function(data, status, headers, config) {
31+
$scope.loading = false;
32+
$scope.status = data.status;
33+
$scope.status_class = "bg-info";
34+
loadContacts();
35+
}).
36+
error(function(data, status, headers, config) {
37+
$scope.loading = false;
38+
$scope.status = "Error - " + data;
39+
$scope.status_class = "bg-danger";
40+
});
41+
};
42+
43+
$scope.saveContact = function(index) {
44+
var contact = $scope.contacts[index];
45+
$scope.edit_flag[index] = false;
46+
$http.put('/api/contact' + contact._id, contact)
47+
.success(function(data) {
48+
loadContacts();
49+
})
50+
.error(function(data) {
51+
console.log('Error: ' + data);
52+
});
53+
};
54+
55+
56+
57+
$scope.deleteContact = function(index) {
58+
59+
var contact_id = $scope.contacts[index]._id;
60+
61+
$http.delete('/api/contact' + contact_id)
62+
.success(function(data) {
63+
loadContacts();
64+
})
65+
.error(function(data) {
66+
console.log('Error: ' + data);
67+
});
68+
};
69+
70+
71+
72+
}

0 commit comments

Comments
 (0)