Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump json5, config and jest #22

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# Vidly_Movie_App
Vidly is a Movie streaming backend application, this application is made using Node Js, Express Js, Mongoose Js. It contains several Models and Routes for building and maintaining endpoints.


Vidly is a Movie rental backend web application, this application is made using Node.js, Express.js, Mongoose.js. You can checkout the documentation below to use it.

## Screenshot
![GitHub Logo](/public/images/image.PNG)
![App Screenshot](https://i.imgur.com/Kpanwvm.png)

## [Check the app](https://vidly-app.herokuapp.com/)
## [Checkout the app](https://vidly-app.herokuapp.com/)

# Running it on your machine
```terminal
$ npm install
$ npm start
```
- **Note**:
1. Make sure to switch to correct node version.
2. You can use [Postman](https://www.postman.com/) to send requests to our web application server.
2 changes: 1 addition & 1 deletion config/custom-environment-variables.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"jwtPrivateKey": "vidly_jwtPrivateKey",
"db": "vidly_db"
}
}
2 changes: 1 addition & 1 deletion config/default.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"jwtPrivateKey": "",
"db": "mongodb://localhost/vidly"
}
}
2 changes: 1 addition & 1 deletion config/test.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"jwtPrivateKey": "1234",
"db": "mongodb://localhost/vidly_tests"
}
}
47 changes: 24 additions & 23 deletions hash.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
const bcrypt = require('bcrypt') // return object
// Trivial module, just to show the usecase of bcrypt to hash passwords
const bcrypt = require('bcrypt');

function run(){
async function hash(){
const salt = await bcrypt.genSalt(10)
const hashedPassword = await bcrypt.hash('1234', salt)
console.log(`Salt: ${salt}`)
console.log(`Hashed Password: ${hashedPassword}\n`)
}
function run() {
async function hash() {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash('1234', salt);
console.log(`Salt: ${salt}`);
console.log(`Hashed Password: ${hashedPassword}\n`);
}

async function countSaltCharacters(){
const salt = await bcrypt.genSalt(10000000000000000000000)
console.log(`Salt: ${salt}`)
console.log(`Salt Length: ${salt.length}`)
}
async function countSaltCharacters() {
const salt = await bcrypt.genSalt(10000000000000000000000);
console.log(`Salt: ${salt}`);
console.log(`Salt Length: ${salt.length}`);
}

async function validate(password){
const salt = await bcrypt.genSalt(10)
const mypassword = '2342'
const hash = await bcrypt.hash(mypassword, salt)
async function validate(password) {
const salt = await bcrypt.genSalt(10);
const mypassword = '2342';
const hash = await bcrypt.hash(mypassword, salt);

const isValid = await bcrypt.compare(password, hash)
console.log(isValid)
}
const isValid = await bcrypt.compare(password, hash);
console.log(isValid);
}

// countSaltCharacters()
validate('2341')
// countSaltCharacters()
validate('2341');
}

run()
run();
12 changes: 7 additions & 5 deletions middleware/admin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = function (req, res, next) {
// req.user
if(!req.user.isAdmin) return res.status(403).send('Access denied')
module.exports = function(req, res, next) {
// req.user
if (!req.user.isAdmin) {
return res.status(403).send('Access denied');
}

next() // if user is admin
}
next(); // if user is admin
};
19 changes: 9 additions & 10 deletions middleware/async.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module.exports = function asyncMiddleware(handler){
return async (req, res, next) => {
try{
await handler(req, res)
next()
}
catch(ex){
next(ex)
}
module.exports = function asyncMiddleware(handler) {
return async (req, res, next) => {
try {
await handler(req, res);
next();
} catch (ex) {
next(ex);
}
}
};
};
30 changes: 15 additions & 15 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// authorisation
const jwt = require('jsonwebtoken')
const config = require('config')
// Authorisation middleware
const jwt = require('jsonwebtoken');
const config = require('config');

module.exports = function (req, res, next) {
const token = req.header('x-auth-token')
module.exports = function(req, res, next) {
const token = req.header('x-auth-token');

if(!token) return res.status(401).send('Access denied. No token provided')
if (!token) return res.status(401).send('Access denied. No token provided');

try{
const decoded = jwt.verify(token, config.get('jwtPrivateKey'))
req.user = decoded // set the user object in the request having decoded jwt
next()
}catch(ex){
res.status(400).send('Invalid Token')
next()
}
}
try {
const decoded = jwt.verify(token, config.get('jwtPrivateKey'));
req.user = decoded; // set the user object in the request having decoded jwt
next();
} catch (ex) {
res.status(400).send('Invalid Token');
next();
}
};
8 changes: 4 additions & 4 deletions middleware/error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const winston = require('winston')
const winston = require('winston');

module.exports = function(err, req, res, next) {
winston.error(err.message, err)
res.status(500).send('Something failed.')
}
winston.error(err.message, err);
res.status(500).send('Something failed.');
};
16 changes: 9 additions & 7 deletions middleware/validate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module.exports = (validator) => {
return (req, res, next) => {
const { error } = validator(req.body)
if(error) return res.status(400).send(error.details[0].message)
module.exports = validator => {
return (req, res, next) => {
const { error } = validator(req.body);
if (error) {
return res.status(400).send(error.details[0].message);
}

next()
}
}
next();
};
};
11 changes: 6 additions & 5 deletions middleware/validateObjectId.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const mongoose = require('mongoose')
const mongoose = require('mongoose');

module.exports = function(req, res, next) {
if(!mongoose.Types.ObjectId.isValid(req.params.id))
return res.status(404).send('Invalid ID')
if (!mongoose.Types.ObjectId.isValid(req.params.id)) {
return res.status(404).send('Invalid ID');
}

next()
}
next();
};
52 changes: 31 additions & 21 deletions models/customer.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
const mongoose = require('mongoose')
const Joi = require('joi')
const mongoose = require('mongoose');
const Joi = require('joi');

const Customer = mongoose.model('Customer', new mongoose.Schema({
const Customer = mongoose.model(
'Customer',
new mongoose.Schema({
isGold: {
type: Boolean,
default: false,
type: Boolean,
default: false
},
name: {
type: String,
required: true
type: String,
required: true
},
phone: {
type: String,
required: true,
minlength: 5,
maxlength: 20
type: String,
required: true,
minlength: 5,
maxlength: 20
}
}))
})
);

function validateCustomer(requestBody){
const schema = {
name: Joi.string().min(3).max(50).required(),
isGold: Joi.boolean(),
phone: Joi.string().min(5).max(25).required()
}
return Joi.validate(requestBody, schema)
function validateCustomer(requestBody) {
const schema = {
name: Joi.string()
.min(3)
.max(50)
.required(),
isGold: Joi.boolean(),
phone: Joi.string()
.min(5)
.max(25)
.required()
};
return Joi.validate(requestBody, schema);
}

module.exports = {
Customer, validateCustomer
}
Customer,
validateCustomer
};
39 changes: 22 additions & 17 deletions models/genre.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
const mongoose = require('mongoose')
const Joi = require('joi')
const mongoose = require('mongoose');
const Joi = require('joi');

const GenreSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 3,
maxlength: 50
}
})
name: {
type: String,
required: true,
minlength: 3,
maxlength: 50
}
});

const Genre = mongoose.model('Genre', GenreSchema)
const Genre = mongoose.model('Genre', GenreSchema);

function validateGenre(reqGenre, objId){
const schema = {
name: Joi.string().min(3).max(50).required()
}
return Joi.validate(reqGenre, schema)
function validateGenre(reqGenre, objId) {
const schema = {
name: Joi.string()
.min(3)
.max(50)
.required()
};
return Joi.validate(reqGenre, schema);
}

module.exports = {
Genre, GenreSchema, validateGenre
}
Genre,
GenreSchema,
validateGenre
};
Loading