Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express')
- var app = express()
- var bodyParser = require('body-parser')
- var MongoClient = require('mongodb').MongoClient;
- var ObjectID = require('mongodb').ObjectID
- var dbUrl = 'mongodb://dilyboi:Password1!@ds225078.mlab.com:25078/flooring';
- var db = null;
- var rx = require('rxjs/Rx');
- var Observable = require('rxjs/Observable').Observable;
- var nodemailer = require('nodemailer');
- require('rxjs/add/observable/of');
- require('rxjs/add/operator/map');
- var smtpTransport = nodemailer.createTransport({
- service: "gmail",
- host: "smtp.gmail.com",
- auth: {
- user: "fantasticfloorrelay@gmail.com",
- pass: "ffRelay77!"
- }
- });
- var db;
- MongoClient.connect(dbUrl, function(err, _db) {
- if (err) throw err;
- db = _db.db('flooring');
- runServer()
- });
- function runServer(){
- app.use( bodyParser.json() );
- app.use(express.static('public/dist'))
- app.use('/assets', express.static('public/dist/assets'))
- app.get('/', function (req, res) {
- res.sendFile(__dirname+'/public/dist/index.html');
- })
- app.get('/favicon.ico', function(req, res){
- res.sendFile(__dirname+'/public/src/favicon.ico')
- })
- app.get('/getManufacturersByType', function(req, res){
- if (!req.query.type) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
- var type = req.query.type
- console.log('request for manufacturers for ', type)
- db.collection("manufacturers").find({type: type}).toArray(function(err, manufacturers){
- res.send(manufacturers)
- });
- })
- app.get('/getManufacturer', function(req, res){
- if (!req.query.manufacturerID) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
- var manufacturerID = req.query.manufacturerID
- console.log('request for manufacturer name for ', manufacturerID)
- db.collection("manufacturers").findOne({_id: ObjectID(manufacturerID)}, (err, manufacturer) => {
- if (err) throw err;
- res.send(manufacturer)
- })
- })
- app.get('/getStylesAndProductsByManufacturer', function(req, res){
- if (!req.query.manufacturerID) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
- var manufacturerID = req.query.manufacturerID
- console.log('request for styles/products for ', manufacturerID)
- db.collection("styles").find({manufacturer: ObjectID(manufacturerID)}).toArray(function(err, styles){
- if (err) throw err;
- res.send(styles);
- });
- });
- app.get('/getTypeByStyleID', function(req, res){
- if (!req.query.styleID) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
- var styleID = req.query.styleID;
- console.log('request for type of ', styleID)
- db.collection("styles").findOne({_id: ObjectID(styleID)}, (err, style) => {
- if (err) throw err;
- db.collection("manufacturers").findOne({_id: style.manufacturer}, (err, manufacturer) => {
- console.log(manufacturer.type)
- res.send({type: manufacturer.type})
- })
- })
- })
- app.get('/getStyle', function(req, res){
- if (!req.query.styleID) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
- var styleID = req.query.styleID
- console.log('request for styles/products for ', styleID)
- db.collection("styles").findOne({_id: ObjectID(styleID)}, (err, style) => {
- if (err) throw err;
- res.send(style);
- });
- })
- app.get('/getProductsByStyle', function(req, res){
- if (!req.query.styleID) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
- db.collection("products").find({style: ObjectID(req.query.styleID)}).toArray(function(err, products){
- if (err) throw err;
- res.send(products)
- })
- })
- app.get('/getProduct', function(req, res){
- if (!req.query.productID) {res.sendStatus(500); console.log('nerd tried to search w/o a param');return}
- db.collection("products").findOne({_id: ObjectID(req.query.productID)}, (err, product) => {
- if (err) throw err;
- if (!product){
- db.collection("styles").findOne({_id: ObjectID(req.query.productID)}, (err, styleProduct)=>{
- if (err) throw err;
- res.send(styleProduct)
- })
- } else {
- res.send(product)
- }
- });
- })
- app.get('/isStyle', function(req, res){
- if (!req.query.styleID) {res.sendStatus(500); console.log('nerd tried to search w/o a param'); return}
- db.collection("products").find({style: ObjectID(req.query.styleID)}).toArray(function(err, products){
- if (err) throw err;
- res.send([products.length > 0, req.query.styleID])
- })
- })
- app.post('/sendMessage', function(req, res){
- 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}
- console.log('got message')
- var subject = req.body.subject;
- var message = req.body.message;
- var from = req.body.from;
- var name = req.body.name;
- console.log(from)
- var mailOptions={
- to : 'support@fantasticfloorsusa.com',
- subject : subject,
- text : 'Message sent from user on fantasticfloorsusa.com\nFrom: '+name+' | '+from+'\nMessage: '+message
- }
- smtpTransport.sendMail(mailOptions, function(error, response){
- if(error){
- console.log(error);
- res.end("error");
- }else{
- console.log("Message sent: " + response.message);
- res.end("sent");
- }
- });
- console.log(subject)
- })
- app.get('*', function (req, res) {
- res.sendFile(__dirname+'/public/dist/index.html');
- })
- app.listen(3000, () => console.log('listening on port 3000'))
- }
- function getManufacturersByType(type, cb){
- db.collection("manufacturers").find({"type": type})
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement