SHARE
TWEET

Untitled

a guest Jun 17th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Route.js
  2. {
  3. var Express         = require("express");
  4. var Routes          = Express.Router();
  5. var UserRoute       = require('./src/User/User.Route');
  6. var CommentRoute    = require('./src/Comment/Comment.Route');
  7.  
  8. Routes.use('/user/', UserRoute);
  9. Routes.use('/comment/', CommentRoute);
  10.  
  11. module.exports = Routes;
  12. }
  13.  
  14. app.js {
  15.  
  16. const Express       = require("express");
  17. const BodyParser    = require("body-parser");
  18. const Routes        = require("./Routes");
  19. const Cors          = require("cors");
  20.  
  21. const app = Express();
  22. app.use(Cors());
  23.  
  24. app.use(BodyParser.urlencoded({ extended: false }));
  25. app.use(BodyParser.json());
  26. app.use('/', Routes);
  27.  
  28. app.listen(8083, 'localhost', (err) => {
  29.     if(err) {
  30.         console.log(err);
  31.         process.exit(-1);
  32.     }
  33.     console.log("Server listen port 8083");
  34. });
  35.      
  36. {
  37. var mongoose        = require('mongoose');
  38. const Schema        = mongoose.Schema;
  39.  
  40. const UserSchema = new Schema({
  41.     userName: {
  42.         type: String,
  43.         require: true
  44.     },
  45.     password: {
  46.         type: String,
  47.         require: true
  48.     }
  49. });
  50.  
  51. mongoose.model('User', UserSchema);
  52. mongoose.model('Comment', CommentSchema);
  53.  
  54. mongoose.connect('mongodb://localhost:27017/comments', (err) => {
  55.     if (err) {
  56.         console.log(err);
  57.         process.exit(-1);
  58.     }
  59.     console.log('Connected to the DB');
  60. });
  61.  
  62. module.exports = mongoose;
  63. }
  64.      
  65. var express     = require('express');
  66. var router      = express.Router();
  67. var Controller  = require('./User.Controller');
  68.  
  69. router.post('/', (req, res) => {
  70.     Controller.insert(req.body).then(data => {
  71.         res.status(data.status).send({message: data.message});
  72.     }).catch(err => {
  73.         res.status(err.status).send({message: err.message});
  74.     })
  75. });
  76.  
  77.  
  78. router.put('/:id', (req, res) => {
  79.     Controller.update(req.params.id, req.body).then(data => {
  80.         res.status(data.status).send({message: data.message});
  81.     }).catch(err => {
  82.         res.status(err.status).send({message: err.message});
  83.     })
  84. });
  85.      
  86. var mongoose        = require('../DBSchema/SchemaMapper');
  87. var UserSchema      = mongoose.model('User');
  88.  
  89. var UserController = function(){
  90.     this.insert = (data) => {
  91.         return new Promise((resolve, reject) => {
  92.             var user = new UserSchema({
  93.                 userName: data.userName,
  94.                 password: data.password
  95.             });
  96.             user.save().then(() => {
  97.                 resolve({status: 200, message: "Added new user"});
  98.             }).catch(err => {
  99.                 reject({status: 500, message: "Error:- "+err});
  100.             })
  101.         })
  102.  
  103.     }
  104.  
  105. this.searchAll = () => {
  106.         return new Promise((resolve, reject) => {
  107.             UserSchema.find().exec().then((data) => {
  108.                 resolve({status: 200, data: data});
  109.             }).catch(err => {
  110.                 reject({status: 500, message: "Error:- " + err});
  111.             })
  112.         })
  113.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top