Advertisement
Guest User

Untitled

a guest
Mar 26th, 2018
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. var express = require('express')
  2. var app = express()
  3. var bodyParser = require('body-parser')
  4.  
  5. var MongoClient = require('mongodb').MongoClient;
  6. var ObjectID = require('mongodb').ObjectID
  7. var dbUrl = 'mongodb://dilyboi:Password1!@ds225078.mlab.com:25078/flooring';
  8. var db = null;
  9.  
  10. var rx = require('rxjs/Rx');
  11. var Observable = require('rxjs/Observable').Observable;
  12. var nodemailer = require('nodemailer');
  13. require('rxjs/add/observable/of');
  14. require('rxjs/add/operator/map');
  15.  
  16. var smtpTransport = nodemailer.createTransport({
  17. service: "gmail",
  18. host: "smtp.gmail.com",
  19. auth: {
  20. user: "fantasticfloorrelay@gmail.com",
  21. pass: "ffRelay77!"
  22. }
  23. });
  24.  
  25. var db;
  26.  
  27. MongoClient.connect(dbUrl, function(err, _db) {
  28. if (err) throw err;
  29. db = _db.db('flooring');
  30. runServer()
  31. });
  32.  
  33. function runServer(){
  34. app.use( bodyParser.json() );
  35. app.use(express.static('public/dist'))
  36. app.use('/assets', express.static('public/dist/assets'))
  37. app.get('/', function (req, res) {
  38. res.sendFile(__dirname+'/public/dist/index.html');
  39. })
  40. app.get('/favicon.ico', function(req, res){
  41. res.sendFile(__dirname+'/public/src/favicon.ico')
  42. })
  43. app.get('/getManufacturersByType', function(req, res){
  44. if (!req.query.type) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
  45. var type = req.query.type
  46. console.log('request for manufacturers for ', type)
  47. db.collection("manufacturers").find({type: type}).toArray(function(err, manufacturers){
  48. res.send(manufacturers)
  49. });
  50. })
  51.  
  52. app.get('/getManufacturer', function(req, res){
  53. if (!req.query.manufacturerID) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
  54. var manufacturerID = req.query.manufacturerID
  55. console.log('request for manufacturer name for ', manufacturerID)
  56. db.collection("manufacturers").findOne({_id: ObjectID(manufacturerID)}, (err, manufacturer) => {
  57. if (err) throw err;
  58. res.send(manufacturer)
  59. })
  60. })
  61.  
  62. app.get('/getStylesAndProductsByManufacturer', function(req, res){
  63. if (!req.query.manufacturerID) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
  64. var manufacturerID = req.query.manufacturerID
  65. console.log('request for styles/products for ', manufacturerID)
  66. db.collection("styles").find({manufacturer: ObjectID(manufacturerID)}).toArray(function(err, styles){
  67. if (err) throw err;
  68. res.send(styles);
  69. });
  70. });
  71.  
  72. app.get('/getTypeByStyleID', function(req, res){
  73. if (!req.query.styleID) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
  74. var styleID = req.query.styleID;
  75. console.log('request for type of ', styleID)
  76. db.collection("styles").findOne({_id: ObjectID(styleID)}, (err, style) => {
  77. if (err) throw err;
  78. db.collection("manufacturers").findOne({_id: style.manufacturer}, (err, manufacturer) => {
  79. console.log(manufacturer.type)
  80. res.send({type: manufacturer.type})
  81. })
  82. })
  83. })
  84.  
  85. app.get('/getStyle', function(req, res){
  86. if (!req.query.styleID) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
  87. var styleID = req.query.styleID
  88. console.log('request for styles/products for ', styleID)
  89. db.collection("styles").findOne({_id: ObjectID(styleID)}, (err, style) => {
  90. if (err) throw err;
  91. res.send(style);
  92. });
  93. })
  94.  
  95. app.get('/getProductsByStyle', function(req, res){
  96. if (!req.query.styleID) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
  97. db.collection("products").find({style: ObjectID(req.query.styleID)}).toArray(function(err, products){
  98. if (err) throw err;
  99. res.send(products)
  100. })
  101. })
  102.  
  103. app.get('/getProduct', function(req, res){
  104. if (!req.query.productID) {res.sendStatus(500); console.log('nerd tried to search w/o a param');return}
  105. db.collection("products").findOne({_id: ObjectID(req.query.productID)}, (err, product) => {
  106. if (err) throw err;
  107. if (!product){
  108. db.collection("styles").findOne({_id: ObjectID(req.query.productID)}, (err, styleProduct)=>{
  109. if (err) throw err;
  110. res.send(styleProduct)
  111. })
  112. } else {
  113. res.send(product)
  114. }
  115. });
  116. })
  117.  
  118. app.get('/isStyle', function(req, res){
  119. if (!req.query.styleID) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
  120. db.collection("products").find({style: ObjectID(req.query.styleID)}).toArray(function(err, products){
  121. if (err) throw err;
  122. res.send([products.length > 0, req.query.styleID])
  123. })
  124. })
  125.  
  126. app.post('/sendMessage', function(req, res){
  127. if (!(req.body.subject && req.body.message && req.body.from && req.body.name)) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
  128. console.log('got message')
  129. var subject = req.body.subject;
  130. var message = req.body.message;
  131. var from = req.body.from;
  132. var name = req.body.name;
  133. console.log(from)
  134. var mailOptions={
  135. to : 'support@fantasticfloorsusa.com',
  136. subject : subject,
  137. text : 'Message sent from user on fantasticfloorsusa.com\nFrom: '+name+' | '+from+'\nMessage: '+message
  138. }
  139.  
  140. smtpTransport.sendMail(mailOptions, function(error, response){
  141. if(error){
  142. console.log(error);
  143. res.end("error");
  144. }else{
  145. console.log("Message sent: " + response.message);
  146. res.end("sent");
  147. }
  148. });
  149. console.log(subject)
  150. })
  151.  
  152. app.get('*', function (req, res) {
  153. res.sendFile(__dirname+'/public/dist/index.html');
  154. })
  155.  
  156. app.listen(3000, () => console.log('listening on port 3000'))
  157.  
  158. }
  159.  
  160. function getManufacturersByType(type, cb){
  161. db.collection("manufacturers").find({"type": type})
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement