daily pastebin goal
5%
SHARE
TWEET

Untitled

a guest Feb 14th, 2018 273 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // set up ======================================================================
  2. var express = require('express');
  3. var path = require('path');
  4. var logger = require('morgan');
  5. var bodyParser = require('body-parser');
  6. var app = express();
  7. var server = require('http').Server(app);
  8. var mongoose = require('mongoose');                 // mongoose for mongodb
  9. var port = process.env.PORT || 8000;                // set the port
  10. var database = require('./config/database');            // load the database config
  11. var morgan = require('morgan');
  12. var methodOverride = require('method-override');
  13. var io = require('socket.io')(server);
  14. var cors = require('cors');
  15. var messageId = {};
  16.  
  17. // configuration ===============================================================
  18. // Connect to DB
  19. mongoose.connect(database.remoteUrl)
  20. mongoose.connection.on('error', function(e) {
  21.     console.log('Can not connect Error:>>',e);
  22.     process.exit();
  23. });
  24. mongoose.connection.once('open', function(d) {
  25.     console.log("Successfully connected to the database");
  26. })
  27. //app.use(express.static('./public'));      // set the static files location /public/img will be /img for users
  28. app.use(morgan('dev')); // log every request to the console
  29. app.use(bodyParser.urlencoded({'extended': 'true'})); // parse application/x-www-form-urlencoded
  30. app.use(bodyParser.json()); // parse application/json
  31. app.use(bodyParser.json({type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json
  32. app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request
  33. app.use(bodyParser.urlencoded({extended:true}))
  34. app.use(bodyParser.json())
  35. app.use(cors());
  36. app.use(function(req, res, next) {
  37.     res.header("Access-Control-Allow-Origin", "*");
  38.     res.header('Access-Control-Allow-Methods', 'DELETE, PUT');
  39.     res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  40.     next();
  41.  });
  42.  
  43. io.set('origins', '*:*');
  44. http = require('http'),
  45. server = http.createServer(function (req, res) {
  46.     //res.writeHead(200,{'content-type':'text/plain'});
  47.     //  res.write("Sever On");
  48.     // res.end();
  49. }),
  50. io = io.listen(server);
  51. io.on('connection', function (socket) {
  52.     console.log('User Connected -- Server Online');  
  53.     socket.on('message', function (msg,msgId) {
  54.         io.emit('message', "Hello");
  55.         console.log("message from client:", msg);
  56.          setInterval(function(){
  57.           io.emit("messageStatus",msgId);
  58.       },500)
  59.    });
  60. });
  61.  
  62. app.use(require('./app/routes.js'));
  63. app.listen(port);
  64. //server.listen(port);
  65. console.log("App listening on port " + port);
  66.    
  67. module.exports = {
  68.     remoteUrl : 'mongodb://username:password@ec2-22-127-21-183.ap-south-1.compute.amazonaws.com:27017/testdb'
  69.  
  70. };
  71.    
  72. var mongoose = require('mongoose');
  73. var Schema = mongoose.Schema;
  74. var ProfileSchema = new Schema({
  75.     id:{
  76.         type:String,
  77.         required:true
  78.     },
  79.     FirstName:{
  80.         type:String,
  81.         required:true
  82.     },
  83.     LastName:{
  84.         type:String,
  85.         required:true
  86.     },
  87.     EmailID:{
  88.         type:String,
  89.         required:true
  90.     },
  91.     Phone:{
  92.         type:Number,
  93.         required:true
  94.     },
  95.     Address1:{
  96.         type:String,
  97.         required:true
  98.     },
  99.     Address2:{
  100.         type:String,
  101.         required:true
  102.     },
  103.     PinCode:{
  104.         type:Number,
  105.         required:true
  106.     },
  107.     Gender:{
  108.         type:String,
  109.         required:true
  110.     },
  111.     ProfilePic:{
  112.         type:String,
  113.         required:true
  114.     },
  115.     IDproof:{
  116.         type:String,
  117.         required:true
  118.     },
  119.     UserName:{
  120.         type:String,
  121.         required:true
  122.     },
  123.     Password:{
  124.         type:String,
  125.         required:true
  126.     },
  127.     Dob:{
  128.         type:Number,
  129.         required:true
  130.     },
  131.     City:{
  132.         type:String,
  133.         required:true
  134.     },
  135.     State:{
  136.         type:String,
  137.         required:true
  138.     },
  139.     HighestEducation:{
  140.         type:String,
  141.         required:true
  142.     }
  143. })
  144. module.exports = mongoose.model('UserProfile',ProfileSchema);
  145.    
  146. var express = require('express')
  147. var app = module.exports = express.Router();
  148. var UserProfile = require('./models/UserProfile');
  149.  
  150.  
  151. app.get('/User', function (req, res) {
  152.     UserProfile.find(function (err, profile) {
  153.         // if there is an error retrieving, send the error. nothing after res.send(err) will execute
  154.         if (err) {
  155.            return res.json({"success":false,"msg":err})
  156.            console.log(err);
  157.         }
  158.         res.status(200).send({"success":true,"result":profile})
  159.    });
  160. });
  161.    
  162. db.User.find().pretty()
  163. {
  164.         "_id" : ObjectId("5a83d1400d47870303228bfa"),
  165.         "FirstName" : "Jazz",
  166.         "LastName" : "Ben",
  167.         "EmailID" : "abc@gmail.com",
  168.         "Phone" : 7654329878,
  169.         "Address1" : "Golden Tulip Est",
  170.         "Address2" : "Earth",
  171.         "PinCode" : 500084,
  172.         "Gender" : "WhoCares",
  173.         "ProfilePic" : "hugyft.in",
  174.         "IDproof" : "hugyc.in",
  175.         "UserName" : "Jazz",
  176.         "Password" : "1736127",
  177.         "Dob" : 1071985,
  178.         "City" : "TheBest",
  179.         "State" : "Solid",
  180.         "HighestEducation" : "PHD"
  181. }
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