clothes.js 3.52 KB
let express = require('express');
let bodyParser = require('body-parser');
let mongoose = require('mongoose');

let cors = require('cors');
let app = express();
app.use(cors());

let logid=null;
const uri = 'mongodb+srv://sjieu17:tjrwls147714@cluster0.lc6pe.mongodb.net/weather_briefing?retryWrites=true&w=majority';
//const uri = 'mongodb+srv://tahmkench:dkrldnsl7@cluster0.vzipl.mongodb.net/?retryWrites=true&w=majority';

let db = mongoose.connect(uri, (err) => {
	if (err) {
		console.log(err.message);
	} else {
		console.log('Succesfully Connected!');
	}
});
console.log(__dirname);
var clothesSchema = new mongoose.Schema({
	gender: Number,
	weather: Number,
  top: Array,
	bottom:Array
});

var Clothes = mongoose.model('clothes', clothesSchema);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ limit: '1gb', extended: false }));

// app.post('/api/addstyple', (req, res) => {
  
//   Clothes.findOne({ gender: req.body.gender, weather: req.body.weather, top:req.top,bottom:req.bottom }, (err, clothes) => {
//     if (err) return res.status(500).json({ registerSuccess: '-1'});
//     else if (clothes){
//             res.json({ registerSuccess: '0'});
//         }
//     else{ 
//           const new_user = new Clothes(req.body);

//           new_user.save((err) => {
//               if (err) return res.status(500).json({ registerSuccess: '-1'});
//               else return res.status(200).json({ registerSuccess: '1'});
//           });
//       }
//   });

// });

app.post('/api/clothes', (req, res) => {
 
  Clothes.findOne({ gender: req.body.gender, weather: req.body.weather }, (err, clothes) => {
    let randt=Math.floor(Math.random()*clothes.top.length);
    let randb=Math.floor(Math.random()*clothes.bottom.length);
		if (err) return res.status(500).json({ MatchingSuccess: false });
		else if (clothes) {
			if(req.body.rain==1) return res.status(200).json({top:"../../../../src/img/"+ clothes.top[randt]+".jpg",bottom:"../../../../src/img/"+ clothes.bottom[randb]+".jpg",umbrella:1,top1:clothes.top[randt],bottom1:clothes.bottom[randb]})
			else return res.status(200).json({top:"../../../../src/img/"+ clothes.top[randt]+".jpg",bottom:"../../../../src/img/"+ clothes.bottom[randb]+".jpg",umbrella:0,top1:clothes.top[randt],bottom1:clothes.bottom[randb]});

        }
		else return res.status(404).json({ MatchingSuccess: false });
	}); 
});

app.listen(4000, () => console.log('Server On 4000'));

































// var express = require('express');
// var app = express();
// var client_id = 'EaHxnjlhHWNjVnHp2eF9';
// var client_secret = 'gPbFbMUxq_';
// app.get('/search/blog', function (req, res) {
//    var api_url = 'https://openapi.naver.com/v1/search/shop.json?query=' + encodeURI(req.query.query); // json 결과
// //   var api_url = 'https://openapi.naver.com/v1/search/blog.xml?query=' + encodeURI(req.query.query); // xml 결과
//    var request = require('request');
//    var options = {
//        url: api_url,
//        headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
//     };
//    request.get(options, function (error, response, body) {
//      if (!error && response.statusCode == 200) {
//        res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'});
//        res.end(body);
//      } else {
//        res.status(response.statusCode).end();
//        console.log('error = ' + response.statusCode);
//      }
//    });
//  });
//  app.listen(4000, function () {
//    console.log('http://127.0.0.1:3000/search/blog?query=검색어 app listening on port 3000!');
//  });

//  //