├── .babelrc ├── .eslintrc ├── .gitignore ├── .prettierrc ├── app.js ├── controller ├── index.js └── user.controller.js ├── database ├── models │ ├── index.js │ └── user.model.js └── schemas │ └── user.schema.js └── package.json /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015", "stage-2"] 3 | } 4 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["airbnb", "prettier"], 3 | "plugins": ["prettier"], 4 | "rules": { 5 | "prettier/prettier": ["error"] 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | package-lock.json 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "es5", 3 | "tabWidth": 2, 4 | "semi": true, 5 | "singleQuote": true 6 | } 7 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | // Import all dependencies & middleware here 2 | import express from 'express'; 3 | import bodyParser from 'body-parser'; 4 | import mongoose from 'mongoose'; 5 | 6 | import { userController } from './controller'; 7 | 8 | // Init an Express App. 9 | const app = express(); 10 | 11 | // Use your dependencies here 12 | app.use(bodyParser.urlencoded({ extended: false })); 13 | app.use(bodyParser.json()); 14 | 15 | // use all controllers(APIs) here 16 | app.use('/', userController); 17 | 18 | // Start Server here 19 | app.listen(8080, () => { 20 | console.log('Server is running on port 8080!'); 21 | mongoose.connect('mongodb://localhost/test').then(() => { 22 | console.log('Conneted to mongoDB at port 27017'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /controller/index.js: -------------------------------------------------------------------------------- 1 | import userController from './user.controller'; 2 | 3 | export { userController }; 4 | -------------------------------------------------------------------------------- /controller/user.controller.js: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import sha256 from 'sha256'; 3 | import { User } from '../database/models'; 4 | 5 | const userController = express.Router(); 6 | 7 | /** 8 | * GET/ 9 | * retrieve and display all Users in the User Model 10 | */ 11 | userController.get('/', (req, res) => { 12 | User.find({}, (err, result) => { 13 | res.status(200).json({ 14 | data: result, 15 | }); 16 | }); 17 | }); 18 | 19 | /** 20 | * POST 21 | * Add a new User to your database 22 | */ 23 | userController.post('/add-user', (req, res) => { 24 | const { email, password } = req.body; 25 | 26 | const userData = { 27 | email, 28 | hashedPassword: sha256(password), 29 | }; 30 | const newUser = new User(userData); 31 | newUser 32 | .save() 33 | .then(data => { 34 | res.status(200).send(data); 35 | }) 36 | .catch(err => { 37 | res.status(400).send('unable to save to database'); 38 | }); 39 | }); 40 | 41 | userController.get('/', (req, res) => { 42 | res.status(200).json({ 43 | status: 'user Controller API call successfully', 44 | }); 45 | }); 46 | 47 | export default userController; 48 | -------------------------------------------------------------------------------- /database/models/index.js: -------------------------------------------------------------------------------- 1 | import User from "./user.model"; 2 | 3 | export { User }; 4 | -------------------------------------------------------------------------------- /database/models/user.model.js: -------------------------------------------------------------------------------- 1 | import mongoose from "mongoose"; 2 | import userSchema from "../schemas/user.schema"; 3 | 4 | const User = mongoose.model("User", userSchema); 5 | 6 | export default User; 7 | -------------------------------------------------------------------------------- /database/schemas/user.schema.js: -------------------------------------------------------------------------------- 1 | import { Schema } from 'mongoose'; 2 | import sha256 from 'sha256'; 3 | 4 | const userSchema = new Schema({ 5 | hashedPassword: { type: String, required: true }, 6 | email: { type: String, required: true }, 7 | }); 8 | 9 | /** 10 | * @param {*} password 11 | */ 12 | userSchema.methods.comparePassword = function comparePassword(password) { 13 | return this.hashedPassword === sha256(password); 14 | }; 15 | 16 | export default userSchema; 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "server", 3 | "version": "1.0.0", 4 | "private": true, 5 | "scripts": { 6 | "start": "node -r esm app.js", 7 | "dev": "nodemon -r esm app.js" 8 | }, 9 | "dependencies": { 10 | "body-parser": "^1.19.0", 11 | "eslint-config-prettier": "^6.9.0", 12 | "eslint-plugin-prettier": "^3.1.2", 13 | "express": "^4.17.1", 14 | "mongoose": "^5.8.3", 15 | "nodemon": "^2.0.2", 16 | "sha256": "^0.2.0" 17 | }, 18 | "devDependencies": { 19 | "babel-cli": "^6.26.0", 20 | "babel-core": "^6.26.3", 21 | "babel-polyfill": "^6.26.0", 22 | "babel-preset-es2015": "^6.24.1", 23 | "babel-preset-stage-2": "^6.24.1", 24 | "babel-register": "^6.26.0", 25 | "esm": "^3.2.25" 26 | } 27 | } 28 | --------------------------------------------------------------------------------