Skip to content

Commit 4247424

Browse files
Add files via upload
1 parent 2975c7f commit 4247424

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
# MongoDB-connection-using-Mongoose-and-Node-JS
2+
# MongoDB-connected-to-NodeJS
3+
# MongoDB-connected-to-NodeJS-Example-1
14
# MongoDB-connected-to-NodeJS-Example-1

Diff for: app.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var mongoose = require('mongoose');
2+
var model = require('./model.js');
3+
var _ = require('underscore');
4+
var express = require('express');
5+
var app = express();
6+
var path = require('path');
7+
var swig = require('swig');
8+
var bodyParser = require('body-parser');
9+
var _ = require("underscore");
10+
11+
// view engine setup
12+
app.set('views', path.join(__dirname, '/view'));
13+
var swig = new swig.Swig();
14+
app.engine('html', swig.renderFile);
15+
app.set('view engine', 'html');
16+
17+
app.use(bodyParser.json({
18+
limit: '100mb'
19+
}));
20+
app.use(bodyParser.urlencoded({
21+
limit: '100mb',
22+
extended: true
23+
}));
24+
25+
26+
27+
28+
mongoose.connect('mongodb://localhost/donarTable', function(err) {
29+
30+
if (err) throw err;
31+
32+
console.log('Successfully connected');
33+
34+
35+
app.get('/', function(req, res) {
36+
37+
model.hospital.find({}, function(err, dbUsers) {
38+
if (err) throw err;
39+
console.log(JSON.stringify(dbUsers));
40+
res.render('index', {
41+
values: dbUsers
42+
});
43+
});
44+
45+
}).listen(8000);
46+
47+
app.post('/addRecipient', function(req, res) {
48+
var body = _.pick(req.body, 'hospitalName', 'hospitalAddress', 'hospitalCity', 'hospitalState', 'hospitalPhoneNumber', 'recipientName', 'recipientBloodGroup', 'acceptableDonorBloodGroup', 'unitRequire', 'bloodNeedIsUrgent')
49+
var hospitalObject = new model.hospital({
50+
hospitalName: body.hospitalName,
51+
hospitalAddress1: body.hospitalAddress,
52+
hospitalAddress2: body.hospitalCity,
53+
hospitalAddress3: body.hospitalState,
54+
hospitalPhoneNumber: body.hospitalPhoneNumber,
55+
RecipientName: body.recipientName,
56+
RecipientBloodGroup: body.recipientBloodGroup,
57+
AcceptableDonorBloodGroup: body.acceptableDonorBloodGroup,
58+
BloodNeedUrgent: body.bloodNeedIsUrgent,
59+
Unit: body.unitRequire
60+
});
61+
62+
hospitalObject.save(function(err) {
63+
if (err) throw err;
64+
model.hospital.find({}, function(err, dbUsers) {
65+
if (err) throw err;
66+
console.log('===>>>' + JSON.stringify(dbUsers));
67+
res.render('index', {
68+
values: dbUsers
69+
});
70+
});
71+
})
72+
73+
})
74+
75+
76+
});

Diff for: model.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var mongoose = require('mongoose');
2+
var Schema = mongoose.Schema;
3+
4+
// create a schema
5+
var hospitalSchema = new Schema({
6+
hospitalName: {
7+
type: String,
8+
required: true
9+
},
10+
hospitalAddress1: { type: String},
11+
hospitalAddress2: { type: String},
12+
hospitalAddress3: { type: String},
13+
hospitalPhoneNumber: { type: Number },
14+
RecipientName: { type: String },
15+
RecipientBloodGroup: { type: String },
16+
AcceptableDonorBloodGroup: { type: String },
17+
BloodNeedUrgent: { type: String },
18+
Unit: { type: Number }
19+
20+
});
21+
22+
23+
var donorSchema = new Schema({
24+
donarName: {
25+
type: String,
26+
required: true
27+
},
28+
donorAddress1: { type: String},
29+
donorAddress2: { type: String},
30+
donorAddress3: { type: String},
31+
donorPhoneNumber: { type: Number },
32+
donorBloodGroup: { type: String },
33+
AcceptableRecipientBloodGroup: { type: String },
34+
BloodDonoteExperience: { type: String },
35+
lastBloodDonatedDate: { type: Date },
36+
hospitalSchemaRef: { type: String }
37+
38+
});
39+
40+
// the schema is useless so far
41+
// we need to create a model using it
42+
var hospital = mongoose.model('Hospital', hospitalSchema);
43+
var donor = mongoose.model('Donor', donorSchema);
44+
45+
// make this available to our users in our Node applications
46+
module.exports.hospital = hospital;
47+
module.exports.donor = donor;

Diff for: package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "mdbtnjs",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"express": "^4.16.3",
13+
"mongodb": "^3.0.7",
14+
"mongoose": "^5.0.15",
15+
"swig": "^1.4.2",
16+
"underscore": "^1.8.3"
17+
}
18+
}

0 commit comments

Comments
 (0)