Skip to content

Build your own online shop server with NodeJS, ExpressJS, and MongoDB [v2.x]

Notifications You must be signed in to change notification settings

EunsooJung/A-Mazon-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A-Mazon-Server

Build your own A-Mazon-Server applicaion with Node.js, Express 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 mongoose, if-env, concurrently

# Run
npm start

Usage

Basic Usage

To get A-Mazon-Server, 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

    [A-Mazon-Server Project Structure]

  • Source Code Check point

  1. folder "models": It provides mongoose 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. helpers: It provides dbErroHaler helper modules.

  2. routes: Server-Side routes

  • Create all of this A-Mazon-Server 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 process application logics with models.
/**
 * @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. server.js:

    • Setup Online-Marketplace web applicaion's environments (npm package dependencies)
    • Import Register the Routers to access.
  2. 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
  • Lucas Coffee
  • Tai Le

License

This project is licensed under the MIT License

Releases

No releases published

Packages

No packages published