김건희

[Merge] 'backend' into 'master'

This diff could not be displayed because it is too large.
1 -{
2 - "name": "weather_briefing",
3 - "version": "0.1.0",
4 - "private": true,
5 - "dependencies": {
6 - "@testing-library/jest-dom": "^5.16.4",
7 - "@testing-library/react": "^13.2.0",
8 - "@testing-library/user-event": "^13.5.0",
9 - "axios": "^0.27.2",
10 - "cors": "^2.8.5",
11 - "express": "^4.18.1",
12 - "http-proxy-middleware": "^2.0.6",
13 - "node-sass": "^7.0.1",
14 - "nodemon": "^2.0.16",
15 - "react": "^18.1.0",
16 - "react-dom": "^18.1.0",
17 - "react-redux": "^8.0.2",
18 - "react-router-dom": "^6.3.0",
19 - "react-scripts": "5.0.1",
20 - "redux": "^4.1.2",
21 - "redux-promise-middleware": "^6.1.2",
22 - "redux-thunk": "^2.4.1",
23 - "web-vitals": "^2.1.4"
24 - },
25 - "scripts": {
26 - "start": "react-scripts start",
27 - "build": "react-scripts build",
28 - "test": "react-scripts test",
29 - "eject": "react-scripts eject"
30 - },
31 - "eslintConfig": {
32 - "extends": [
33 - "react-app",
34 - "react-app/jest"
35 - ]
36 - },
37 - "browserslist": {
38 - "production": [
39 - ">0.2%",
40 - "not dead",
41 - "not op_mini all"
42 - ],
43 - "development": [
44 - "last 1 chrome version",
45 - "last 1 firefox version",
46 - "last 1 safari version"
47 - ]
48 - },
49 - "devDependencies": {
50 - "redux-devtools-extension": "^2.13.9"
51 - }
52 -}
No preview for this file type
1 +let express = require('express');
2 +let bodyParser = require('body-parser');
3 +let mongoose = require('mongoose');
4 +var request = require('request');
5 +let session = require('express-session');
6 +let cors = require('cors');
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());
11 +app.use(cors());
12 +
13 +let logData=null;
14 +const uri = 'mongodb+srv://sjieu17:tjrwls147714@cluster0.lc6pe.mongodb.net/weather_briefing?retryWrites=true&w=majority';
15 +var queryParams = '?' + encodeURIComponent('serviceKey') + '=3OcUyvx97Vx2YikiZ9IHyRQ6suapku7Xn8VlefQKQWrGIFOGaejhbevwagcubdHfSiQAqJwCV5lyIutw0%2BsppA%3D%3D'; /* Service Key*/
16 +// const uri = 'mongodb+srv://tahmkench:dkrldnsl7@cluster0.vzipl.mongodb.net/?retryWrites=true&w=majority';
17 +
18 +let db = mongoose.connect(uri, (err) => {
19 + if (err) {
20 + console.log(err.message);
21 + } else {
22 + console.log('Succesfully Connected!');
23 + }
24 +});
25 +
26 +var UserSchema = new mongoose.Schema({
27 + name: String,
28 + id: String,
29 + password: String,
30 + gender:String
31 +});
32 +
33 +let mongo = require('mongoose');
34 +
35 +let hi = mongo.connect(uri, (err) => {
36 + if (err) {
37 + console.log(err.message);
38 + } else {
39 + console.log('Succesfully Connected!');
40 + }
41 +});
42 +
43 +var clothesSchema = new mongo.Schema({
44 + gender: Number,
45 + weather: Number,
46 + top: Array,
47 + bottom:Array
48 +});
49 +
50 +var Users = mongoose.model('users', UserSchema);
51 +
52 +app.use(bodyParser.json());
53 +app.use(bodyParser.urlencoded({ limit: '1gb', extended: false }));
54 +
55 +app.post('/api/register', (req, res) => {
56 +
57 + Users.findOne({ id: req.body.id, password: req.body.password }, (err, user) => {
58 + if (err) return res.status(500).json({ registerSuccess: '-1'});
59 + else if (user){
60 + res.json({ registerSuccess: '0'});
61 + }
62 + else{
63 + const new_user = new Users(req.body);
64 +
65 + new_user.save((err) => {
66 + if (err) return res.status(500).json({ registerSuccess: '-1'});
67 + else return res.status(200).json({ registerSuccess: '1'});
68 + });
69 + }
70 + });
71 +
72 +});
73 +
74 +app.post('/api/login', (req, res) => {
75 +
76 + Users.findOne({ id: req.body.id, password: req.body.password }, (err, user) => {
77 + if (err) return res.status(500).json({ loginSuccess: false });
78 + else if (user) {
79 + logData={name:user.name,gender:user.gender};
80 + return res.status(200).json({ loginSuccess: true, logData });
81 + }
82 + else return res.status(404).json({ loginSuccess: false });
83 + });
84 +});
85 +
86 +app.post('/api/logout',(req,res)=>{
87 + return res.status(200).json({ loginSuccess: false });
88 +});
89 +
90 +//변수들
91 +// let curaddress;
92 +
93 +
94 +//오늘의 날짜 구하기
95 +let base,tommorow;
96 +
97 +let today=new Date();
98 +let CurDay=today.getFullYear().toString();
99 +if(today.getMonth()<9){
100 + CurDay+="0"+(today.getMonth()+1).toString();
101 +}
102 +else{
103 + CurDay+=(today.getMonth()+1).toString();
104 +}
105 +if(today.getDate()<10){
106 + base=CurDay+"0"+(today.getDate()-1).toString();
107 + tommorow=CurDay+"0"+(today.getDate()+1).toString();
108 + if(today.getDate()==9){
109 + tommorow=CurDay+(today.getDate()+1).toString();
110 +
111 + }
112 +
113 +
114 + CurDay+="0"+today.getDate().toString();
115 +}
116 +else{
117 + base=CurDay+(today.getDate()-1).toString();
118 + tommorow=CurDay+(today.getDate()+1).toString();
119 +
120 + CurDay+=today.getDate().toString();
121 +}
122 +
123 +//nx,ny구하기
124 +const xlsx=require('xlsx')
125 +const excel=xlsx.readFile('location.xlsx');
126 +const sheet=excel.SheetNames[0];
127 +const first=excel.Sheets[sheet];
128 +const jsonData=xlsx.utils.sheet_to_json(first,{defval:""});
129 +let nx,ny;
130 +
131 +app.post('/api/address', (req, res) => {
132 + const addressList = [];
133 +
134 + for (let i = 0; i < jsonData.length; i++) {
135 + addressList.push({
136 + address1 : jsonData[i].address1,
137 + address2 : jsonData[i].address2,
138 + address3 : jsonData[i].address3,
139 + dotX : jsonData[i].nx,
140 + dotY : jsonData[i].ny,
141 + });
142 + }
143 +
144 + res.json(addressList);
145 +})
146 +
147 +let a3=[], a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
148 +
149 +app.post('/api/weather',(req,res)=>{
150 + queryParams = '?' + encodeURIComponent('serviceKey') + '=3OcUyvx97Vx2YikiZ9IHyRQ6suapku7Xn8VlefQKQWrGIFOGaejhbevwagcubdHfSiQAqJwCV5lyIutw0%2BsppA%3D%3D';
151 + var url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst';
152 + const nx = req.body.dotX;
153 + const ny = req.body.dotY;
154 + queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
155 + queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('1000'); /* */
156 + queryParams += '&' + encodeURIComponent('dataType') + '=' + encodeURIComponent('JSON'); /* */
157 + queryParams += '&' + encodeURIComponent('base_date') + '=' + encodeURIComponent(base); /* */
158 + queryParams += '&' + encodeURIComponent('base_time') + '=' + encodeURIComponent('2359'); /* */
159 + queryParams += '&' + encodeURIComponent('nx') + '=' + encodeURIComponent(nx); /*nx*/
160 + queryParams += '&' + encodeURIComponent('ny') + '=' + encodeURIComponent(ny); /*ny*/
161 + request({
162 + url: url + queryParams,
163 + method: 'GET'
164 + }, function (error, response, body) {
165 + let ex=JSON.parse(body);
166 + let item=ex.response.body.items.item;
167 + let i=0;
168 + let a=[];//, a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
169 + while(item[i].fcstDate==CurDay){
170 + if(item[i].category=='POP' || item[i].category=='TMP'){
171 + a.push(item[i]);
172 + }
173 + i+=1;
174 + }
175 + let result=[];
176 + i=0;
177 + let Json;
178 + while(i<a.length){
179 + if(a[i].fcstValue<=8){
180 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:0};
181 + }
182 + else if(a[i].fcstValue>8 && a[i].fcstValue<=12){
183 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:1};
184 + }
185 + else if(a[i].fcstValue>12 && a[i].fcstValue<=18){
186 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:2};
187 + }
188 + else if(a[i].fcstValue>18 && a[i].fcstValue<=23){
189 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:3};
190 + }
191 + else{
192 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:4};
193 + };
194 + result.push(Json);
195 + i+=2;
196 + }
197 + let addressData = {
198 + address1 : req.body.address1,
199 + address2 : req.body.address2,
200 + address3 : req.body.address3
201 + }
202 + result.push(addressData);
203 + res.json(result);
204 + });//pop:강수확률 tmp:한시간 기온
205 +});
206 +
207 +app.post('/api/tommorrow',(req,res)=>{
208 + queryParams = '?' + encodeURIComponent('serviceKey') + '=3OcUyvx97Vx2YikiZ9IHyRQ6suapku7Xn8VlefQKQWrGIFOGaejhbevwagcubdHfSiQAqJwCV5lyIutw0%2BsppA%3D%3D';
209 + var url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst';
210 + const nx = req.body.dotX;
211 + const ny = req.body.dotY;
212 +
213 + queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
214 + queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('1000'); /* */
215 + queryParams += '&' + encodeURIComponent('dataType') + '=' + encodeURIComponent('JSON'); /* */
216 + queryParams += '&' + encodeURIComponent('base_date') + '=' + encodeURIComponent(base); /* */
217 + queryParams += '&' + encodeURIComponent('base_time') + '=' + encodeURIComponent('2359'); /* */
218 + queryParams += '&' + encodeURIComponent('nx') + '=' + encodeURIComponent(nx); /*nx*/
219 + queryParams += '&' + encodeURIComponent('ny') + '=' + encodeURIComponent(ny); /*ny*/
220 + request({
221 + url: url + queryParams,
222 + method: 'GET'
223 + }, function (error, response, body) {
224 + let ex=JSON.parse(body);
225 + let item=ex.response.body.items.item;
226 + let i=0;
227 + let a=[];//, a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
228 +
229 + while(item[i].fcstDate==tommorow||item[i].fcstDate==CurDay){
230 + if((item[i].category=='POP' || item[i].category=='TMP') && item[i].fcstDate==tommorow){
231 + a.push(item[i]);
232 + }
233 + i+=1;
234 + }
235 + let result=[];
236 + i=0;
237 + let Json;
238 + while(i<a.length){
239 + if(a[i].fcstValue<=8){
240 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:0};
241 + }
242 + else if(a[i].fcstValue>8 && a[i].fcstValue<=12){
243 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:1};
244 + }
245 + else if(a[i].fcstValue>12 && a[i].fcstValue<=18){
246 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:2};
247 + }
248 + else if(a[i].fcstValue>18 && a[i].fcstValue<=23){
249 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:3};
250 + }
251 + else{
252 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:4};
253 + };
254 + result.push(Json);
255 + i+=2;
256 + }
257 + res.json(result);
258 + });//pop:강수확률 tmp:한시간 기온
259 +});
260 +
261 +// let cors = require('cors');
262 +// let app = express();
263 +
264 +
265 +// const uri = 'mongodb+srv://sjieu17:tjrwls147714@cluster0.lc6pe.mongodb.net/weather_briefing?retryWrites=true&w=majority';
266 +//const uri = 'mongodb+srv://tahmkench:dkrldnsl7@cluster0.vzipl.mongodb.net/?retryWrites=true&w=majority';
267 +
268 +var Clothes = mongo.model('clothes', clothesSchema);
269 +
270 +app.use(bodyParser.json());
271 +app.use(bodyParser.urlencoded({ limit: '1gb', extended: false }));
272 +
273 +app.post('/api/clothes', (req, res) => {
274 +
275 + Clothes.findOne({ gender: req.body.gender, weather: req.body.weather }, (err, clothes) => {
276 + let randt=Math.floor(Math.random()*clothes.top.length);
277 + let randb=Math.floor(Math.random()*clothes.bottom.length);
278 + if (err) return res.status(500).json({ MatchingSuccess: false });
279 + else if (clothes) {
280 + if(req.body.rain==1) return res.status(200).json({top:"../../../../../img/"+ clothes.top[randt]+".jpg",bottom:"../../../../../img/"+ clothes.bottom[randb]+".jpg",umbrella:1})
281 + else return res.status(200).json({top:"../../../../../img/"+ clothes.top[randt]+".jpg",bottom:"../../../../../img/"+ clothes.bottom[randb]+".jpg",umbrella:0});
282 +
283 + }
284 + else return res.status(404).json({ MatchingSuccess: false });
285 + });
286 +});
287 +
288 +app.listen(4000, () => console.log('Server On 4000'));
...\ No newline at end of file ...\ No newline at end of file