Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
NenoR96 committed Mar 2, 2019
0 parents commit ab2433b
Show file tree
Hide file tree
Showing 33 changed files with 2,389 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
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
```
83 changes: 83 additions & 0 deletions app.js
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...');
});
8 changes: 8 additions & 0 deletions config/config.js
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'
}

34 changes: 34 additions & 0 deletions config/passport.js
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);
});
});
}
12 changes: 12 additions & 0 deletions models/reservation.js
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);
14 changes: 14 additions & 0 deletions models/room.js
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);
12 changes: 12 additions & 0 deletions models/user.js
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);
Loading

0 comments on commit ab2433b

Please sign in to comment.