EunsooJung / Online-Marketplace

creating a marketplace that allows users to view products. The user will be able to view details about the product, and if they decide, add the product to a shopping cart.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Online-Marketplace

Build your own Online-Marketplace web applicaion with ReactJS, Node.js and MongoDB with MVC Pattern.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

# Install packages
npm i axios, mongoose, if-env, concurrently

# Run
npm start

Preview

Online-Marketplace

Usage

Basic Usage

To get Online-Marketplace, after downloading, you need to make sure Git Bash terminal open and looking at the correct folder. When you are within the correct location, you may type the following commands to ask her for information:

  • npm start

Guidelines:

  • Proceeds as follows:

To use this applicaion, Clone the applicaion to your local git repository or directory:

To start:

  • You have to install npm packages depend on my package.json file: "npm install"
  • Open your terminal then "npm start"

Code Snippet

  • Project structure

    [Online-Marketplace Project Structure]

  • Source Code Check point

  1. folder "models": It provides sequelize Schema model
const mongoose = require('mongoose');
const crypto = require('crypto');
const uuidv1 = require('uuid/v1');

const userSchema = new mongoose.Schema(
  {
    //
    name: {
      type: String,
      trim: true,
      require: true,
      maxlength: 32
    },

    email: {
      type: String,
      trim: true,
      require: true,
      unique: 32
    },
    // virtual filed
    hashed_password: {
      type: String,
      required: true
    },
    // User profile
    about: {
      type: String,
      trim: true
    },
    // salt needs unique string
    salt: String,
    role: {
      type: Number,
      default: 0
    },
    // user purchased history
    history: {
      type: Array,
      default: []
    }
  },
  { timestamps: true }
);

// virtual field for clien side after install uuid
userSchema
  .virtual('password')
  .set(function(password) {
    this._password = password;
    // salt gives us random string as the hashed password
    this.salt = uuidv1();
    this.hashed_password = this.encryptPassword(password);
  })
  .get(function() {
    return this._password;
  });

// Create userSchema method to apply encryptPassword
userSchema.methods = {
  // Create authenticate method in user model
  authenticate: function(plainText) {
    return this.encryptPassword(plainText) === this.hashed_password;
  },

  encryptPassword: function(password) {
    if (!password) return '';
    try {
      return crypto
        .createHmac('sha1', this.salt)
        .update(password)
        .digest('hex');
    } catch (err) {
      return '';
    }
  }
};

module.exports = mongoose.model('User', userSchema);
  1. folder "public": It provides images and css files

  2. routes: Server-Side routes

  • Create all of this Shopping Cart web application's routes (maps) using a exppress router.
/*  Get user authentication */
router.get('/secret/:userId', requireSignin, isAuth, isAdmin, (req, res) => {
  res.json({
    user: req.profile
  });
});
  1. Controller layer:
  • It provide application logic to comunicate with client and ORM.
/**
 * @method userById
 * @Description This method will run automatically and make the user available in the request object.
 * To redirect them to the user desperate and you want to display the basic information.
 */
exports.userById = (req, res, next, id) => {
  User.findById(id).exec((err, user) => {
    if (err || !user) {
      return res.status(400).json({
        error: 'User not found'
      });
    }
    req.profile = user;
    // go to next page
    next();
  });
};
  1. app.js:

    • Setup Online-Marketplace web applicaion's environments (npm package dependencies)
    • Import Register the Routers to access.
  2. views: View layer to represent user interface on Client-Side React Framework.

  3. validator: It provides helper method to validate user data on signup process,apply to user routes

exports.userSignupValidator = (req, res, next) => {
  req.check('name', 'Name is required').notEmpty();
  req
    .check('email', 'Email must be between 3 to 32 characters')
    .matches(/.+\@.+\..+/)
    .withMessage('Email must contain @')
    .isLength({
      min: 4,
      max: 32
    });
  req.check('password', 'Password is required').notEmpty();
  req
    .check('password')
    .isLength({ min: 6 })
    .withMessage('Password must contain at least 6 characters')
    .matches(/\d/)
    .withMessage('Password must contain a number');
  const errors = req.validationErrors();
  if (errors) {
    const firstError = errors.map(error => error.msg)[0];
    return res.status(400).json({ error: firstError });
  }
  next();
};

Built With

Authors

  • Michael(Eunsoo)Jung

License

This project is licensed under the MIT License

About

creating a marketplace that allows users to view products. The user will be able to view details about the product, and if they decide, add the product to a shopping cart.


Languages

Language:JavaScript 100.0%