Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
katjuell committed Feb 7, 2019
1 parent 1676a9a commit c8100dd
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 6 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
@@ -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!')
Expand Down
29 changes: 29 additions & 0 deletions controllers/sharks.js
Original file line number Diff line number Diff line change
@@ -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
});
});
};
11 changes: 11 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
@@ -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});
9 changes: 9 additions & 0 deletions models/sharks.js
Original file line number Diff line number Diff line change
@@ -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)
157 changes: 157 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"express"
],
"dependencies": {
"express": "^4.16.4"
"ejs": "^2.6.1",
"express": "^4.16.4",
"mongoose": "^5.4.10"
}
}
14 changes: 14 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 17 additions & 0 deletions routes/sharks.js
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit c8100dd

Please sign in to comment.