-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit ab2433b
Showing
33 changed files
with
2,389 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Article Knowledgebase Application | ||
|
||
This is a knowledgebase app used in the "Node.js & Express From Scratch" Youtube series. | ||
|
||
## Technologies | ||
* Node.js | ||
* Express | ||
* Express Messages, Session, Connect Flash & Validation | ||
* MongoDB & Mongoose | ||
* Pug Templating | ||
* Passport.js Authentication | ||
* BCrypt Hashing | ||
|
||
### Version | ||
2.0.0 | ||
|
||
## Usage | ||
|
||
|
||
### Installation | ||
|
||
Install the dependencies | ||
|
||
```sh | ||
$ npm install | ||
``` | ||
Run app | ||
|
||
```sh | ||
$ npm start | ||
``` |
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,83 @@ | ||
const express = require('express'); | ||
const path = require('path'); | ||
const expressHbs = require('express-handlebars'); | ||
const mongoose = require('mongoose'); | ||
const bodyParser = require('body-parser'); | ||
const expressValidator = require('express-validator'); | ||
const session = require('express-session'); | ||
const passport = require('passport'); | ||
const hbs = require('handlebars'); | ||
const moment = require('moment'); | ||
|
||
mongoose.connect('mongodb://localhost:27017/roomsdb'); | ||
//================================================================================================ | ||
const app = express(); | ||
app.set('views',path.join(__dirname,'views')) | ||
app.engine('.hbs', expressHbs({defaultLayout: 'layout', extname: '.hbs'})); | ||
app.set('view engine', '.hbs'); | ||
//HANDLEBARS====================================================================================== | ||
hbs.registerHelper('ifEquals', function(a, b, options) { | ||
if (a === b) { | ||
return options.fn(this); | ||
} | ||
return options.inverse(this); | ||
}); | ||
//================================================================================================ | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(bodyParser.json()); | ||
app.use(express.static('public')); | ||
//EXPRESS-SESSION================================================================================= | ||
app.use(session({ | ||
secret: 'keyboard cat', | ||
resave: true, | ||
saveUninitialized: true | ||
})); | ||
|
||
app.use(function (req, res, next) { | ||
res.locals.messages = require('express-messages')(req, res); | ||
next(); | ||
}); | ||
|
||
app.use(expressValidator({ | ||
errorFormatter: function(param, msg, value) { | ||
var namespace = param.split('.') | ||
, root = namespace.shift() | ||
, formParam = root; | ||
|
||
while(namespace.length) { | ||
formParam += '[' + namespace.shift() + ']'; | ||
} | ||
return { | ||
param : formParam, | ||
msg : msg, | ||
value : value | ||
}; | ||
} | ||
})); | ||
//PASSPORT======================================================================================== | ||
require('./config/passport')(passport); | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
app.get('*', function(req, res, next){ | ||
res.locals.user = req.user || null; | ||
next(); | ||
}); | ||
//================================================================================================ | ||
let index = require('./routes/index'); | ||
let users = require('./routes/users'); | ||
let rooms = require('./routes/rooms'); | ||
let admin = require('./routes/admin'); | ||
app.use('/', index); | ||
app.use('/user', users); | ||
app.use('/room', rooms); | ||
app.use('/admin', admin); | ||
//================================================================================================ | ||
app.post('/endpoint', function(req, res){ | ||
var obj = {}; | ||
console.log('body: ' + JSON.stringify(req.body)); | ||
res.send(req.body); | ||
}); | ||
|
||
app.listen(3000, function(){ | ||
console.log('Server started on port 3000...'); | ||
}); |
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,8 @@ | ||
module.exports = { | ||
database:'mongodb://localhost:27017/roomsdb', | ||
eservice: 'gmail', | ||
esecret: 'asdf1093KMnzxcvnkljvasdu09123nlasdasdf', | ||
email: '[email protected]', | ||
epass: 'somethingwrong' | ||
} | ||
|
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,34 @@ | ||
const LocalStrategy = require('passport-local').Strategy; | ||
const User = require('../models/user'); | ||
const bcrypt = require('bcryptjs'); | ||
|
||
module.exports = function(passport){ | ||
passport.use(new LocalStrategy(function(username, password, done){ | ||
let query = {username:username}; | ||
User.findOne(query, function(err, user){ | ||
if(err) throw err; | ||
if(!user){ | ||
return done(null, false, {message: 'No user found'}); | ||
} | ||
|
||
bcrypt.compare(password, user.password, function(err, isMatch){ | ||
if(err) throw err; | ||
if(isMatch){ | ||
return done(null, user); | ||
} else { | ||
return done(null, false, {message: 'Wrong password'}); | ||
} | ||
}); | ||
}); | ||
})); | ||
|
||
passport.serializeUser(function(user, done) { | ||
done(null, user.id); | ||
}); | ||
|
||
passport.deserializeUser(function(id, done) { | ||
User.findById(id, function(err, user) { | ||
done(err, user); | ||
}); | ||
}); | ||
} |
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,12 @@ | ||
const mongoose = require('mongoose'); | ||
// const User = require('../models/user'); | ||
|
||
const ReservationSchema = new mongoose.Schema({ | ||
user: { type: mongoose.Schema.Types.ObjectId, required: true}, | ||
room: { type: mongoose.Schema.Types.ObjectId, required: true}, | ||
from: { type: String, required: true}, | ||
to: { type: String, required: true}, | ||
state: {type: Number, required: true} | ||
}); | ||
|
||
const Reservation = module.exports = mongoose.model('Reservation', ReservationSchema); |
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 mongoose = require('mongoose'); | ||
|
||
const RoomSchema = new mongoose.Schema({ | ||
type:{ type: String, required: true }, | ||
number:{ type: Number, required: true }, | ||
beds:{ type: Number, required: true }, | ||
capacity:{ type: Number, required: true }, | ||
cost:{ type: Number, required: true }, | ||
reservation: [ {type: mongoose.Schema.Types.ObjectId, required: false} ], | ||
img: { type: String, required: true }, | ||
images: [ {type: String, required: false} ] | ||
}); | ||
|
||
const Room = module.exports = mongoose.model('Room', RoomSchema); |
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,12 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const UserSchema = mongoose.Schema({ | ||
name:{ type: String, required: true }, | ||
email:{ type: String, required: true }, | ||
username:{ type: String, required: true }, | ||
password:{ type: String, required: true }, | ||
admin:{ type: Boolean, required: false }, | ||
img:{ type: String, required: true } | ||
}); | ||
|
||
const User = module.exports = mongoose.model('User', UserSchema); |
Oops, something went wrong.