Toggle navigation
Toggle navigation
This project
Loading...
Sign in
신동해
/
Omniscient-Public-Point-Of-View
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
4
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
신동해
2021-12-09 08:41:04 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
9bf936af74deb09e0552b784946bc95a75827e52
9bf936af
1 parent
fdb8ba60
Update server.js : add filtering function of lib/pro news, make api-request dynamic
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
98 additions
and
47 deletions
server.js
server.js
View file @
9bf936a
...
...
@@ -3,65 +3,116 @@ const app = express();
const
bodyParser
=
require
(
'body-parser'
)
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
true
}));
app
.
set
(
'view engine'
,
'ejs'
);
app
.
use
(
'/public'
,
express
.
static
(
'public'
));
const
fs
=
require
(
'fs'
);
// JSON 파일 읽기
// 네이버 검색 Open API 예제 - 블로그 검색
var
client_id
=
'Q5XjQPuQgvUh7MQCyBHx'
;
var
client_secret
=
'dF9OQ6AqrV'
;
// DB 연결
var
db
;
const
MongoClient
=
require
(
'mongodb'
).
MongoClient
;
MongoClient
.
connect
(
'mongodb+srv://admin:qwer1234@cluster0.m2pvs.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'
,
function
(
에러
,
cli
ne
t
){
// 연결되면 할
일
MongoClient
.
connect
(
'mongodb+srv://admin:qwer1234@cluster0.m2pvs.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'
,
function
(
에러
,
cli
en
t
){
// 연결되면 할
결
if
(
에러
){
return
console
.
log
(
에러
);
}
db
=
clinet
.
db
(
'todoapp'
);
app
.
listen
(
8080
,
function
(){
console
.
log
(
'listening on 8080'
)
});
});
else
{
db
=
client
.
db
(
'OPPOV'
);
app
.
get
(
'/pet'
,
function
(
요청
,
응답
){
응답
.
send
(
'펫쇼핑할 수 있는 페이지입니다.'
);
});
app
.
get
(
'/beauty'
,
function
(
요청
,
응답
){
응답
.
send
(
'뷰티용품 쇼핑할 수 있는 페이지입니다.'
);
app
.
listen
(
8080
,
function
(){
console
.
log
(
'listening on 8080'
)
});
}
});
// 메인 페이지
app
.
get
(
'/'
,
function
(
요청
,
응답
){
응답
.
sendFile
(
__dirname
+
'/index.html
'
);
})
;
응답
.
render
(
'index.ejs
'
);
})
app
.
get
(
'/write'
,
function
(
요청
,
응답
){
응답
.
sendFile
(
__dirname
+
'/write.html'
);
// 진보,보수 동시 검색 페이지
app
.
get
(
'/list'
,
function
(
요청
,
응답
){
응답
.
render
(
'list.ejs'
,
{
주제
:
요청
.
query
.
subject
});
});
app
.
post
(
'/add'
,
function
(
요청
,
응답
){
응답
.
send
(
'전송완료'
);
db
.
collection
(
'counter'
).
findOne
({
name
:
'게시물갯수'
},
function
(
에러
,
결과
){
console
.
log
(
결과
.
totalPost
)
var
총게시물갯수
=
결과
.
totalPost
db
.
collection
(
'post'
).
insertOne
({
_id
:
총게시물갯수
+
1
,
제목
:
요청
.
body
.
title
,
날짜
:
요청
.
body
.
date
},
function
(
에러
,
결과
){
console
.
log
(
'저장완료'
)
// counter라는 콜렉션에 있는 totalPost라는 항목도 1 증가시켜야함
db
.
collection
(
'counter'
).
updateOne
({
name
:
'게시물갯수'
},{
$inc
:
{
totalPost
:
1
}
},
function
(){
if
(
에러
){
return
console
.
log
(
에러
)
}
})
});
// 보수 측 네이버 뉴스 검색
app
.
get
(
'/result/liberal/:subject'
,
function
(
req
,
res
)
{
var
api_url
=
'https://openapi.naver.com/v1/search/news.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'
);
const
option
=
{
query
:
req
.
params
.
subject
,
start
:
1
,
display
:
100
,
sort
:
'sim'
}
var
options
=
{
url
:
api_url
,
qs
:
option
,
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
)
{
const
jsonData
=
JSON
.
parse
(
body
);
const
result
=
jsonData
;
// 보수 뉴스만 걸러내기
var
i
;
var
news
=
[];
for
(
i
=
0
;
i
<
parseInt
(
result
.
display
);
i
++
){
var
con
=
result
.
items
[
i
].
originallink
.
includes
(
'chosun'
)
|
result
.
items
[
i
].
originallink
.
includes
(
'joongang'
)
|
result
.
items
[
i
].
originallink
.
includes
(
'donga'
)
if
(
con
){
news
.
push
(
result
.
items
[
i
])
}
else
{
continue
;
}
}
res
.
render
(
'result_lib.ejs'
,
{
posts
:
news
,
num
:
news
.
length
})
}
else
{
res
.
status
(
response
.
statusCode
).
end
();
console
.
log
(
'error = '
+
response
.
statusCode
);
}
});
});
})
app
.
get
(
'/list'
,
function
(
요청
,
응답
){
// DB에 저장된 post라는 collectino안의 ~~인 데이터를 꺼내주세요
db
.
collection
(
'post'
).
find
().
toArray
(
function
(
에러
,
결과
){
console
.
log
(
결과
);
응답
.
render
(
'list.ejs'
,
{
posts
:
결과
});
})
});
app
.
delete
(
'/delete'
,
function
(
요청
,
응답
){
console
.
log
(
요청
.
body
)
})
\ No newline at end of file
// 진보 측 네이버 뉴스 검색
app
.
get
(
'/result/progress/:subject'
,
function
(
req
,
res
)
{
var
api_url
=
'https://openapi.naver.com/v1/search/news.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'
);
const
option
=
{
query
:
req
.
params
.
subject
,
start
:
1
,
display
:
100
,
sort
:
'sim'
}
var
options
=
{
url
:
api_url
,
qs
:
option
,
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
)
{
const
jsonData
=
JSON
.
parse
(
body
);
const
result
=
jsonData
;
// 진보뉴스만 걸러내기
var
i
;
var
news
=
[];
for
(
i
=
0
;
i
<
parseInt
(
result
.
display
);
i
++
){
var
con
=
result
.
items
[
i
].
originallink
.
includes
(
'hani'
)
|
result
.
items
[
i
].
originallink
.
includes
(
'khan'
)
|
result
.
items
[
i
].
originallink
.
includes
(
'ohmynews'
)
if
(
con
){
news
.
push
(
result
.
items
[
i
])
}
else
{
continue
;
}
}
res
.
render
(
'result_pro.ejs'
,
{
posts
:
news
,
num
:
news
.
length
})
}
else
{
res
.
status
(
response
.
statusCode
).
end
();
console
.
log
(
'error = '
+
response
.
statusCode
);
}
});
});
\ No newline at end of file
...
...
Please
register
or
login
to post a comment