├── .dockerignore ├── README.md ├── models └── sharks.js ├── Dockerfile ├── routes ├── index.js └── sharks.js ├── db.js ├── package.json ├── app.js ├── views ├── css │ └── styles.css ├── index.html ├── getshark.html └── sharks.html ├── controllers └── sharks.js └── .gitignore /.dockerignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | Dockerfile 4 | .dockerignore 5 | .git 6 | README.md 7 | .gitignore 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Project code for tutorial on integrating MongoDB into Node.js application using Mongoose: https://www.digitalocean.com/community/tutorials/how-to-integrate-mongodb-with-your-node-application 2 | -------------------------------------------------------------------------------- /models/sharks.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | const Schema = mongoose.Schema; 3 | 4 | const Shark = new Schema ({ 5 | name: { type: String, required: true }, 6 | character: { type: String, required: true }, 7 | }); 8 | 9 | module.exports = mongoose.model('Shark', Shark) 10 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:10-alpine 2 | 3 | RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app 4 | 5 | WORKDIR /home/node/app 6 | 7 | COPY package*.json ./ 8 | 9 | USER node 10 | 11 | RUN npm install 12 | 13 | COPY --chown=node:node . . 14 | 15 | EXPOSE 8080 16 | 17 | CMD [ "node", "app.js" ] 18 | -------------------------------------------------------------------------------- /routes/index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const router = express.Router(); 3 | const path = require('path'); 4 | 5 | router.use (function (req,res,next) { 6 | console.log('/' + req.method); 7 | next(); 8 | }); 9 | 10 | router.get('/',function(req,res){ 11 | res.sendFile(path.resolve('views/index.html')); 12 | }); 13 | 14 | module.exports = router; 15 | -------------------------------------------------------------------------------- /db.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | 3 | const MONGO_USERNAME = 'sammy'; 4 | const MONGO_PASSWORD = 'password'; 5 | const MONGO_HOSTNAME = '127.0.0.1'; 6 | const MONGO_PORT = '27017'; 7 | const MONGO_DB = 'sharkinfo'; 8 | 9 | const url = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?authSource=admin`; 10 | 11 | mongoose.connect(url, {useNewUrlParser: true}); 12 | -------------------------------------------------------------------------------- /routes/sharks.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const router = express.Router(); 3 | const shark = require('../controllers/sharks'); 4 | 5 | router.get('/', function(req, res){ 6 | shark.index(req,res); 7 | }); 8 | 9 | router.post('/addshark', function(req, res) { 10 | shark.create(req,res); 11 | }); 12 | 13 | router.get('/getshark', function(req, res) { 14 | shark.list(req,res); 15 | }); 16 | 17 | module.exports = router; 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-image-demo", 3 | "version": "1.0.0", 4 | "description": "nodejs image demo", 5 | "author": "katjuell ", 6 | "license": "MIT", 7 | "main": "app.js", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/katjuell/nodejs-image-demo.git" 11 | }, 12 | "keywords": [ 13 | "nodejs", 14 | "bootstrap", 15 | "express" 16 | ], 17 | "dependencies": { 18 | "ejs": "^2.6.1", 19 | "express": "^4.16.4", 20 | "mongoose": "^5.4.10" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | const router = express.Router(); 4 | const db = require('./db'); 5 | const sharks = require('./routes/sharks'); 6 | 7 | const path = __dirname + '/views/'; 8 | const port = 8080; 9 | 10 | app.engine('html', require('ejs').renderFile); 11 | app.set('view engine', 'html'); 12 | app.use(express.urlencoded({ extended: true })); 13 | app.use(express.static(path)); 14 | app.use('/sharks', sharks); 15 | 16 | app.listen(port, function () { 17 | console.log('Example app listening on port 8080!') 18 | }) 19 | -------------------------------------------------------------------------------- /views/css/styles.css: -------------------------------------------------------------------------------- 1 | .navbar { 2 | margin-bottom: 0; 3 | background: #000000; 4 | } 5 | 6 | body { 7 | background: #000000; 8 | color: #ffffff; 9 | font-family: 'Merriweather', sans-serif; 10 | } 11 | 12 | h1, 13 | h2 { 14 | font-weight: bold; 15 | } 16 | 17 | p { 18 | font-size: 16px; 19 | color: #ffffff; 20 | } 21 | 22 | .jumbotron { 23 | background: #0048CD; 24 | color: white; 25 | text-align: center; 26 | } 27 | 28 | .jumbotron p { 29 | color: white; 30 | font-size: 26px; 31 | } 32 | 33 | .btn-primary { 34 | color: #fff; 35 | text-color: #000000; 36 | border-color: white; 37 | margin-bottom: 5px; 38 | } 39 | 40 | img, 41 | video, 42 | audio { 43 | margin-top: 20px; 44 | max-width: 80%; 45 | } 46 | 47 | div.caption: { 48 | float: left; 49 | clear: both; 50 | } 51 | -------------------------------------------------------------------------------- /controllers/sharks.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const Shark = require('../models/sharks'); 3 | 4 | exports.index = function (req, res) { 5 | res.sendFile(path.resolve('views/sharks.html')); 6 | }; 7 | 8 | exports.create = function (req, res) { 9 | var newShark = new Shark(req.body); 10 | console.log(req.body); 11 | newShark.save(function (err) { 12 | if(err) { 13 | res.status(400).send('Unable to save shark to database'); 14 | } else { 15 | res.redirect('/sharks/getshark'); 16 | } 17 | }); 18 | }; 19 | 20 | exports.list = function (req, res) { 21 | Shark.find({}).exec(function (err, sharks) { 22 | if (err) { 23 | return res.send(500, err); 24 | } 25 | res.render('getshark', { 26 | sharks: sharks 27 | }); 28 | }); 29 | }; 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # parcel-bundler cache (https://parceljs.org/) 61 | .cache 62 | 63 | # next.js build output 64 | .next 65 | 66 | # nuxt.js build output 67 | .nuxt 68 | 69 | # vuepress build output 70 | .vuepress/dist 71 | 72 | # Serverless directories 73 | .serverless/ 74 | 75 | # FuseBox cache 76 | .fusebox/ 77 | 78 | #DynamoDB Local files 79 | .dynamodb/ 80 | -------------------------------------------------------------------------------- /views/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | About Sharks 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 28 |
29 |
30 |

Want to Learn About Sharks?

31 |

Are you ready to learn about sharks?

32 |
33 |

Get Shark Info 34 |

35 |
36 |
37 |
38 |
39 |
40 |

Not all sharks are alike

41 |

Though some are dangerous, sharks generally do not attack humans. Out of the 500 species known to researchers, only 30 have been known to attack humans. 42 |

43 |
44 |
45 |

Sharks are ancient

46 |

There is evidence to suggest that sharks lived up to 400 million years ago. 47 |

48 |
49 |
50 |
51 | 52 | 53 | -------------------------------------------------------------------------------- /views/getshark.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | About Sharks 6 | 7 | 8 | 9 | 10 | 11 | 12 | 26 |
27 |

Shark Info

28 |
29 |
30 |
31 |
32 |

33 |

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. 34 |
35 | Sawshark 36 |

37 |
38 |
39 |

40 |

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

43 |
44 |
45 |

46 |

Your Sharks
47 |
    48 | <% sharks.forEach(function(shark) { %> 49 |

    Name: <%= shark.name %>

    50 |

    Character: <%= shark.character %>

    51 | <% }); %> 52 |
53 |

54 |
55 |
56 |
57 | 58 | -------------------------------------------------------------------------------- /views/sharks.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | About Sharks 6 | 7 | 8 | 9 | 10 | 11 | 12 | 26 |
27 |

Shark Info

28 |
29 |
30 |
31 |
32 |

33 |

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. 34 |
35 | Sawshark 36 |

37 |
38 |
39 |

40 |

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

43 |
44 |
45 |

46 |

47 |
Enter Your Shark
48 | 49 | 50 | 51 |
52 |

53 |
54 |
55 |
56 | 57 | --------------------------------------------------------------------------------