diff --git a/README.md b/README.md index d6ea56d..908a43d 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -Minimal Node.js application for intro to Docker tutorial: https://www.digitalocean.com/community/tutorials/how-to-build-a-node-js-application-with-docker +Project code for tutorial on integrating MongoDB into Node.js application using Mongoose. diff --git a/app.js b/app.js index 1a85793..703039c 100644 --- a/app.js +++ b/app.js @@ -1,25 +1,17 @@ const express = require('express'); const app = express(); const router = express.Router(); +const db = require('./db'); +const sharks = require('./routes/sharks'); const path = __dirname + '/views/'; const port = 8080; -router.use(function (req,res,next) { - console.log('/' + req.method); - next(); -}); - -router.get('/',function(req,res){ - res.sendFile(path + 'index.html'); -}); - -router.get('/sharks',function(req,res){ - res.sendFile(path + 'sharks.html'); -}); - +app.engine('html', require('ejs').renderFile); +app.set('view engine', 'html'); +app.use(express.urlencoded({ extended: true })); app.use(express.static(path)); -app.use('/', router); +app.use('/sharks', sharks); app.listen(port, function () { console.log('Example app listening on port 8080!') diff --git a/controllers/sharks.js b/controllers/sharks.js new file mode 100644 index 0000000..c812576 --- /dev/null +++ b/controllers/sharks.js @@ -0,0 +1,29 @@ +const path = require('path'); +const Shark = require('../models/sharks'); + +exports.index = function (req, res) { + res.sendFile(path.resolve('views/sharks.html')); +}; + +exports.create = function (req, res) { + var newShark = new Shark(req.body); + console.log(req.body); + newShark.save(function (err) { + if(err) { + res.status(400).send('Unable to save shark to database'); + } else { + res.redirect('/sharks/getshark'); + } + }); + }; + +exports.list = function (req, res) { + Shark.find({}).exec(function (err, sharks) { + if (err) { + return res.send(500, err); + } + res.render('getshark', { + sharks: sharks + }); + }); +}; diff --git a/db.js b/db.js new file mode 100644 index 0000000..683c4c0 --- /dev/null +++ b/db.js @@ -0,0 +1,11 @@ +const mongoose = require('mongoose'); + +const MONGO_USERNAME = 'mrcat'; +const MONGO_PASSWORD = 'brow789*()+'; +const MONGO_HOSTNAME = '127.0.0.1'; +const MONGO_PORT = '27017'; +const MONGO_DB = 'sharkinfo'; + +const url = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?authSource=admin`; + +mongoose.connect(url, {useNewUrlParser: true}); diff --git a/models/sharks.js b/models/sharks.js new file mode 100644 index 0000000..bcf1b37 --- /dev/null +++ b/models/sharks.js @@ -0,0 +1,9 @@ +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; + +const Shark = new Schema ({ + name: { type: String, required: true }, + character: { type: String, required: true }, +}); + +module.exports = mongoose.model('Shark', Shark) diff --git a/package-lock.json b/package-lock.json index 80a2c90..2d9d1ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,19 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "async": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/async/-/async-2.6.1.tgz", + "integrity": "sha512-fNEiL2+AZt6AlAw/29Cr0UDe4sRAHCpEHh54WMz+Bb7QfNcFw4h3loofyJpLeQs4Yx7yuqu/2dLgM5hKOs6HlQ==", + "requires": { + "lodash": "^4.17.10" + } + }, + "bluebird": { + "version": "3.5.1", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", + "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==" + }, "body-parser": { "version": "1.18.3", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.18.3.tgz", @@ -35,6 +48,11 @@ "type-is": "~1.6.16" } }, + "bson": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.0.tgz", + "integrity": "sha512-9Aeai9TacfNtWXOYarkFJRW2CWo+dRon+fuLZYJmvLV3+MiUp0bEI6IAZfXEIg7/Pl/7IWlLaDnhzTsD81etQA==" + }, "bytes": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", @@ -83,6 +101,11 @@ "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" }, + "ejs": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/ejs/-/ejs-2.6.1.tgz", + "integrity": "sha512-0xy4A/twfrRCnkhfk8ErDi5DqdAsAqeGxht4xkCUrsvhhbQNs7E+4jV0CN7+NKIY0aHE72+XvqtBIXzD31ZbXQ==" + }, "encodeurl": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", @@ -188,11 +211,27 @@ "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.8.0.tgz", "integrity": "sha1-6qM9bd16zo9/b+DJygRA5wZzix4=" }, + "kareem": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.0.tgz", + "integrity": "sha512-6hHxsp9e6zQU8nXsP+02HGWXwTkOEw6IROhF2ZA28cYbUk4eJ6QbtZvdqZOdD9YPKghG3apk5eOCvs+tLl3lRg==" + }, + "lodash": { + "version": "4.17.11", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", + "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" + }, "media-typer": { "version": "0.3.0", "resolved": "http://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" }, + "memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "optional": true + }, "merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -221,6 +260,77 @@ "mime-db": "~1.37.0" } }, + "mongodb": { + "version": "3.1.13", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.1.13.tgz", + "integrity": "sha512-sz2dhvBZQWf3LRNDhbd30KHVzdjZx9IKC0L+kSZ/gzYquCF5zPOgGqRz6sSCqYZtKP2ekB4nfLxhGtzGHnIKxA==", + "requires": { + "mongodb-core": "3.1.11", + "safe-buffer": "^5.1.2" + } + }, + "mongodb-core": { + "version": "3.1.11", + "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.1.11.tgz", + "integrity": "sha512-rD2US2s5qk/ckbiiGFHeu+yKYDXdJ1G87F6CG3YdaZpzdOm5zpoAZd/EKbPmFO6cQZ+XVXBXBJ660sSI0gc6qg==", + "requires": { + "bson": "^1.1.0", + "require_optional": "^1.0.1", + "safe-buffer": "^5.1.2", + "saslprep": "^1.0.0" + } + }, + "mongoose": { + "version": "5.4.10", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.4.10.tgz", + "integrity": "sha512-yJl+A7p8QF++tyBl6fRGFON5GV57MTXLKNiaiqgX5vZs1qJpcZpPiUGU5odBiqTHFYPybuRp8dW9Qtv4K6SxBQ==", + "requires": { + "async": "2.6.1", + "bson": "~1.1.0", + "kareem": "2.3.0", + "mongodb": "3.1.13", + "mongodb-core": "3.1.11", + "mongoose-legacy-pluralize": "1.0.2", + "mpath": "0.5.1", + "mquery": "3.2.0", + "ms": "2.0.0", + "regexp-clone": "0.0.1", + "safe-buffer": "5.1.2", + "sliced": "1.0.1" + } + }, + "mongoose-legacy-pluralize": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz", + "integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ==" + }, + "mpath": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.5.1.tgz", + "integrity": "sha512-H8OVQ+QEz82sch4wbODFOz+3YQ61FYz/z3eJ5pIdbMEaUzDqA268Wd+Vt4Paw9TJfvDgVKaayC0gBzMIw2jhsg==" + }, + "mquery": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-3.2.0.tgz", + "integrity": "sha512-qPJcdK/yqcbQiKoemAt62Y0BAc0fTEKo1IThodBD+O5meQRJT/2HSe5QpBNwaa4CjskoGrYWsEyjkqgiE0qjhg==", + "requires": { + "bluebird": "3.5.1", + "debug": "3.1.0", + "regexp-clone": "0.0.1", + "safe-buffer": "5.1.2", + "sliced": "1.0.1" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + } + } + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -279,6 +389,25 @@ "unpipe": "1.0.0" } }, + "regexp-clone": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/regexp-clone/-/regexp-clone-0.0.1.tgz", + "integrity": "sha1-p8LgmJH9vzj7sQ03b7cwA+aKxYk=" + }, + "require_optional": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", + "integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==", + "requires": { + "resolve-from": "^2.0.0", + "semver": "^5.1.0" + } + }, + "resolve-from": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz", + "integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=" + }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", @@ -289,6 +418,20 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "saslprep": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.2.tgz", + "integrity": "sha512-4cDsYuAjXssUSjxHKRe4DTZC0agDwsCqcMqtJAQPzC74nJ7LfAJflAtC1Zed5hMzEQKj82d3tuzqdGNRsLJ4Gw==", + "optional": true, + "requires": { + "sparse-bitfield": "^3.0.3" + } + }, + "semver": { + "version": "5.6.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.6.0.tgz", + "integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==" + }, "send": { "version": "0.16.2", "resolved": "https://registry.npmjs.org/send/-/send-0.16.2.tgz", @@ -325,6 +468,20 @@ "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.0.tgz", "integrity": "sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ==" }, + "sliced": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/sliced/-/sliced-1.0.1.tgz", + "integrity": "sha1-CzpmK10Ewxd7GSa+qCsD+Dei70E=" + }, + "sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=", + "optional": true, + "requires": { + "memory-pager": "^1.0.2" + } + }, "statuses": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", diff --git a/package.json b/package.json index 8682531..3d0f59d 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,8 @@ "express" ], "dependencies": { - "express": "^4.16.4" + "ejs": "^2.6.1", + "express": "^4.16.4", + "mongoose": "^5.4.10" } } diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..0dd98f5 --- /dev/null +++ b/routes/index.js @@ -0,0 +1,14 @@ +const express = require('express'); +const router = express.Router(); +const path = require('path'); + +router.use (function (req,res,next) { + console.log('/' + req.method); + next(); +}); + +router.get('/',function(req,res){ + res.sendFile(path.resolve('views/index.html')); +}); + +module.exports = router; diff --git a/routes/sharks.js b/routes/sharks.js new file mode 100644 index 0000000..6038f71 --- /dev/null +++ b/routes/sharks.js @@ -0,0 +1,17 @@ +const express = require('express'); +const router = express.Router(); +const shark = require('../controllers/sharks'); + +router.get('/', function(req, res){ + shark.index(req,res); +}); + +router.post('/addshark', function(req, res) { + shark.create(req,res); +}); + +router.get('/getshark', function(req, res) { + shark.list(req,res); +}); + +module.exports = router; diff --git a/views/getshark.html b/views/getshark.html new file mode 100644 index 0000000..9db1d09 --- /dev/null +++ b/views/getshark.html @@ -0,0 +1,57 @@ + + + + + About Sharks + + + + + + + +
+

Shark Info

+
+
+
+
+

+

Some sharks are known to be dangerous to humans, though many more are not. The sawshark, for example, is not considered a threat to humans. +
+ Sawshark +

+
+
+

+

Other sharks are known to be friendly and welcoming!
+ Sammy the Shark +

+
+
+

+

Your Sharks
+
    + <% sharks.forEach(function(shark) { %> +

    Name: <%= shark.name %>

    +

    Character: <%= shark.character %>

    + <% }); %> +
+

+
+
+
+ diff --git a/views/sharks.html b/views/sharks.html index d043a3b..6c4b274 100644 --- a/views/sharks.html +++ b/views/sharks.html @@ -28,19 +28,29 @@

Shark Info

-
+

Some sharks are known to be dangerous to humans, though many more are not. The sawshark, for example, is not considered a threat to humans.
Sawshark

-
+

Other sharks are known to be friendly and welcoming!
Sammy the Shark

+
+

+

+
Enter Your Shark
+ + + +
+

+