forked from katjuell/nodejs-mongo-mongoose
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
316 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.