Seokjin

[UPDATE]merge into server.js

1 let express = require('express'); 1 let express = require('express');
2 let bodyParser = require('body-parser'); 2 let bodyParser = require('body-parser');
3 -let mongoose = require('mongoose'); 3 +let mongo = require('mongoose');
4 4
5 let cors = require('cors'); 5 let cors = require('cors');
6 let app = express(); 6 let app = express();
7 app.use(cors()); 7 app.use(cors());
8 8
9 -let logid=null; 9 +
10 const uri = 'mongodb+srv://sjieu17:tjrwls147714@cluster0.lc6pe.mongodb.net/weather_briefing?retryWrites=true&w=majority'; 10 const uri = 'mongodb+srv://sjieu17:tjrwls147714@cluster0.lc6pe.mongodb.net/weather_briefing?retryWrites=true&w=majority';
11 //const uri = 'mongodb+srv://tahmkench:dkrldnsl7@cluster0.vzipl.mongodb.net/?retryWrites=true&w=majority'; 11 //const uri = 'mongodb+srv://tahmkench:dkrldnsl7@cluster0.vzipl.mongodb.net/?retryWrites=true&w=majority';
12 12
13 -let db = mongoose.connect(uri, (err) => { 13 +let hi = mongo.connect(uri, (err) => {
14 if (err) { 14 if (err) {
15 console.log(err.message); 15 console.log(err.message);
16 } else { 16 } else {
17 console.log('Succesfully Connected!'); 17 console.log('Succesfully Connected!');
18 } 18 }
19 }); 19 });
20 -console.log(__dirname); 20 +var clothesSchema = new mongo.Schema({
21 -var clothesSchema = new mongoose.Schema({
22 gender: Number, 21 gender: Number,
23 weather: Number, 22 weather: Number,
24 top: Array, 23 top: Array,
25 bottom:Array 24 bottom:Array
26 }); 25 });
27 26
28 -var Clothes = mongoose.model('clothes', clothesSchema); 27 +var Clothes = mongo.model('clothes', clothesSchema);
29 28
30 app.use(bodyParser.json()); 29 app.use(bodyParser.json());
31 app.use(bodyParser.urlencoded({ limit: '1gb', extended: false })); 30 app.use(bodyParser.urlencoded({ limit: '1gb', extended: false }));
32 31
33 -// app.post('/api/addstyple', (req, res) => { 32 +
34 -
35 -// Clothes.findOne({ gender: req.body.gender, weather: req.body.weather, top:req.top,bottom:req.bottom }, (err, clothes) => {
36 -// if (err) return res.status(500).json({ registerSuccess: '-1'});
37 -// else if (clothes){
38 -// res.json({ registerSuccess: '0'});
39 -// }
40 -// else{
41 -// const new_user = new Clothes(req.body);
42 -
43 -// new_user.save((err) => {
44 -// if (err) return res.status(500).json({ registerSuccess: '-1'});
45 -// else return res.status(200).json({ registerSuccess: '1'});
46 -// });
47 -// }
48 -// });
49 -
50 -// });
51 33
52 app.post('/api/clothes', (req, res) => { 34 app.post('/api/clothes', (req, res) => {
53 35
......
1 let express = require('express'); 1 let express = require('express');
2 let bodyParser = require('body-parser'); 2 let bodyParser = require('body-parser');
3 let mongoose = require('mongoose'); 3 let mongoose = require('mongoose');
4 - 4 +var request = require('request');
5 +let session = require('express-session');
5 let cors = require('cors'); 6 let cors = require('cors');
6 let app = express(); 7 let app = express();
8 +app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
9 +app.use(bodyParser.urlencoded({ extended: false }));
10 +app.use(bodyParser.json());
7 app.use(cors()); 11 app.use(cors());
8 12
9 let logid=null; 13 let logid=null;
...@@ -54,7 +58,6 @@ app.post('/api/login', (req, res) => { ...@@ -54,7 +58,6 @@ app.post('/api/login', (req, res) => {
54 Users.findOne({ id: req.body.id, password: req.body.password }, (err, user) => { 58 Users.findOne({ id: req.body.id, password: req.body.password }, (err, user) => {
55 if (err) return res.status(500).json({ loginSuccess: false }); 59 if (err) return res.status(500).json({ loginSuccess: false });
56 else if (user) { 60 else if (user) {
57 - // logid={id:user.id,password:user.password,name:user.name,gender:user.gender};
58 return res.status(200).json({ loginSuccess: true }); 61 return res.status(200).json({ loginSuccess: true });
59 } 62 }
60 else return res.status(404).json({ loginSuccess: false }); 63 else return res.status(404).json({ loginSuccess: false });
...@@ -66,4 +69,196 @@ app.post('/api/logout',(req,res)=>{ ...@@ -66,4 +69,196 @@ app.post('/api/logout',(req,res)=>{
66 res.send('로그아웃 되었습니다.'); 69 res.send('로그아웃 되었습니다.');
67 }); 70 });
68 71
72 +//변수들
73 +// let curaddress;
74 +
75 +
76 +//오늘의 날짜 구하기
77 +let base,tommorow;
78 +
79 +let today=new Date();
80 +let CurDay=today.getFullYear().toString();
81 +if(today.getMonth()<9){
82 + CurDay+="0"+(today.getMonth()+1).toString();
83 +}
84 +else{
85 + CurDay+=(today.getMonth()+1).toString();
86 +}
87 +if(today.getDate()<10){
88 + base=CurDay+"0"+(today.getDate()-1).toString();
89 + tommorow=CurDay+"0"+(today.getDate()+1).toString();
90 +
91 + CurDay+="0"+today.getDate().toString();
92 +}
93 +else{
94 + base=CurDay+(today.getDate()-1).toString();
95 + tommorow=CurDay+(today.getDate()+1).toString();
96 +
97 + CurDay+=today.getDate().toString();
98 +}
99 +console.log(tommorow);
100 +//nx,ny구하기
101 +const xlsx=require('xlsx')
102 +const excel=xlsx.readFile('location.xlsx');
103 +const sheet=excel.SheetNames[0];
104 +const first=excel.Sheets[sheet];
105 +const jsonData=xlsx.utils.sheet_to_json(first,{defval:""});
106 +let nx,ny;
107 +app.post('/api/address', (req, res) => {
108 + let i=0;
109 + while(i<3788){
110 + if(jsonData[i].address1==req.body.address1 && jsonData[i].address2==req.body.address2 && jsonData[i].address3==req.body.address3){
111 + nx=jsonData[i].nx;
112 + ny=jsonData[i].ny;
113 + curaddress=jsonData[i];
114 + res.json({nx, ny});
115 + break;
116 + }
117 + i+=1;
118 + }
119 +});
120 +
121 +let a3=[], a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
122 +//입력받기
123 +app.post('/api/weather',(req,res)=>{
124 + let Nx = req.body.dotX;
125 + let Ny = req.body.dotY;
126 + var queryParams = '?' + encodeURIComponent('serviceKey') + '=3OcUyvx97Vx2YikiZ9IHyRQ6suapku7Xn8VlefQKQWrGIFOGaejhbevwagcubdHfSiQAqJwCV5lyIutw0%2BsppA%3D%3D'; /* Service Key*/
127 + var url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst';
128 +
129 + queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
130 + queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('1000'); /* */
131 + queryParams += '&' + encodeURIComponent('dataType') + '=' + encodeURIComponent('JSON'); /* */
132 + queryParams += '&' + encodeURIComponent('base_date') + '=' + encodeURIComponent(base); /* */
133 + queryParams += '&' + encodeURIComponent('base_time') + '=' + encodeURIComponent('2300'); /* */
134 + queryParams += '&' + encodeURIComponent('nx') + '=' + encodeURIComponent(Nx); /*nx*/
135 + queryParams += '&' + encodeURIComponent('ny') + '=' + encodeURIComponent(Ny); /*ny*/
136 +
137 + request({
138 + url: url + queryParams,
139 + method: 'GET'
140 + }, function (error, response, body) {
141 + let ex=JSON.parse(body);
142 + let item=ex.response.body.items.item;
143 +
144 + let i=0;
145 + let a=[];//, a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
146 + while(item[i].fcstDate==CurDay){
147 + if(item[i].category=='POP' || item[i].category=='TMP'){
148 + a.push(item[i]);
149 + }
150 + i+=1;
151 + }
152 + let result=[];
153 + i=0;
154 + let Json;
155 + while(i<a.length){
156 + if(a[i].fcstValue<=8){
157 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:0};
158 + }
159 + else if(a[i].fcstValue>8 && a[i].fcstValue<=12){
160 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:1};
161 + }
162 + else if(a[i].fcstValue>12 && a[i].fcstValue<=18){
163 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:2};
164 + }
165 + else if(a[i].fcstValue>18 && a[i].fcstValue<=23){
166 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:3};
167 + }
168 + else{
169 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:4};
170 + };
171 + result.push(Json);
172 + i+=2;
173 + }
174 + res.json(result);
175 + });//pop:강수확률 tmp:한시간 기온
176 +});
177 +
178 +app.post('/api/tomorrow',(req,res)=>{
179 + const nx = req.body.dotX;
180 + const ny = req.body.dotY;
181 + var queryParams = '?' + encodeURIComponent('serviceKey') + '=3OcUyvx97Vx2YikiZ9IHyRQ6suapku7Xn8VlefQKQWrGIFOGaejhbevwagcubdHfSiQAqJwCV5lyIutw0%2BsppA%3D%3D'; /* Service Key*/
182 + var url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst';
183 +
184 + queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
185 + queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('1000'); /* */
186 + queryParams += '&' + encodeURIComponent('dataType') + '=' + encodeURIComponent('JSON'); /* */
187 + queryParams += '&' + encodeURIComponent('base_date') + '=' + encodeURIComponent(base); /* */
188 + queryParams += '&' + encodeURIComponent('base_time') + '=' + encodeURIComponent('2300'); /* */
189 + queryParams += '&' + encodeURIComponent('nx') + '=' + encodeURIComponent(nx); /*nx*/
190 + queryParams += '&' + encodeURIComponent('ny') + '=' + encodeURIComponent(ny); /*ny*/
191 +
192 + request({
193 + url: url + queryParams,
194 + method: 'GET'
195 + }, function (error, response, body) {
196 + let ex=JSON.parse(body);
197 + let item=ex.response.body.items.item;
198 +
199 + let i=0;
200 + let a=[];//, a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
201 + while(item[i].fcstDate==tommorow||item[i].fcstDate==CurDay){
202 + if((item[i].category=='POP' || item[i].category=='TMP') && item[i].fcstDate==tommorow){
203 + a.push(item[i]);
204 + }
205 + i+=1;
206 + }
207 + let result=[];
208 + i=0;
209 + let Json;
210 + while(i<a.length){
211 + if(a[i].fcstValue<=8){
212 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:0};
213 + }
214 + else if(a[i].fcstValue>8 && a[i].fcstValue<=12){
215 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:1};
216 + }
217 + else if(a[i].fcstValue>12 && a[i].fcstValue<=18){
218 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:2};
219 + }
220 + else if(a[i].fcstValue>18 && a[i].fcstValue<=23){
221 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:3};
222 + }
223 + else{
224 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:4};
225 + };
226 + result.push(Json);
227 + i+=2;
228 + }
229 + res.json(result);
230 + });//pop:강수확률 tmp:한시간 기온
231 +});
232 +
233 +let mongo = require('mongoose');
234 +
235 +// let cors = require('cors');
236 +// let app = express();
237 +app.use(cors());
238 +
239 +
240 +// const uri = 'mongodb+srv://sjieu17:tjrwls147714@cluster0.lc6pe.mongodb.net/weather_briefing?retryWrites=true&w=majority';
241 +//const uri = 'mongodb+srv://tahmkench:dkrldnsl7@cluster0.vzipl.mongodb.net/?retryWrites=true&w=majority';
242 +
243 +let hi = mongo.connect(uri, (err) => {
244 + if (err) {
245 + console.log(err.message);
246 + } else {
247 + console.log('Succesfully Connected!');
248 + }
249 +});
250 +var clothesSchema = new mongo.Schema({
251 + gender: Number,
252 + weather: Number,
253 + top: Array,
254 + bottom:Array
255 +});
256 +
257 +var Clothes = mongo.model('clothes', clothesSchema);
258 +
259 +app.use(bodyParser.json());
260 +app.use(bodyParser.urlencoded({ limit: '1gb', extended: false }));
261 +
262 +
263 +
69 app.listen(4000, () => console.log('Server On 4000')); 264 app.listen(4000, () => console.log('Server On 4000'));
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -173,4 +173,33 @@ app.post('/api/tomorrow',(req,res)=>{ ...@@ -173,4 +173,33 @@ app.post('/api/tomorrow',(req,res)=>{
173 });//pop:강수확률 tmp:한시간 기온 173 });//pop:강수확률 tmp:한시간 기온
174 }); 174 });
175 175
176 +var clothesSchema = new mongoose.Schema({
177 + gender: Number,
178 + weather: Number,
179 + top: Array,
180 + bottom:Array
181 +});
182 +
183 +var Clothes = mongoose.model('clothes', clothesSchema);
184 +
185 +app.use(bodyParser.json());
186 +app.use(bodyParser.urlencoded({ limit: '1gb', extended: false }));
187 +
188 +
189 +
190 +app.post('/api/clothes', (req, res) => {
191 +
192 + Clothes.findOne({ gender: req.body.gender, weather: req.body.weather }, (err, clothes) => {
193 + let randt=Math.floor(Math.random()*clothes.top.length);
194 + let randb=Math.floor(Math.random()*clothes.bottom.length);
195 + if (err) return res.status(500).json({ MatchingSuccess: false });
196 + else if (clothes) {
197 + 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]})
198 + 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]});
199 +
200 + }
201 + else return res.status(404).json({ MatchingSuccess: false });
202 + });
203 +});
204 +
176 app.listen(4000, () => console.log('Server On 4000')); 205 app.listen(4000, () => console.log('Server On 4000'));
...\ No newline at end of file ...\ No newline at end of file
......