clothes.js
3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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) {
return res.status(200).json({top:"../../../../src/img/"+ clothes.top[randt]+".jpg",bottom:"../../../../src/img/"+ clothes.bottom[randb]+".jpg"});
}
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!');
// });
// //