임승현

Merge branch 'feature/Search' into 'feature/Search'

영화 검색 API_json 파싱



See merge request !2
1 +var express = require('express');
2 +var config = require('./config.json')
3 +var app = express();
4 +var client_id = config.SearchClientID;
5 +var client_secret = config.SearchClientSecret;
6 +
7 +
8 +var SearchWord;
9 +var MovieSearchParse;
10 +var movietitlelist;
11 +var movielist;
12 +app.post('/search/movie', function (req, res) {SearchWord = req.query.query; res.send(SearchWord)})
13 +app.get('/search/movie', function (req, res) {
14 + var api_url = 'https://openapi.naver.com/v1/search/movie?query=' + encodeURI(SearchWord);
15 + var request = require('request');
16 + var options = {
17 + url: api_url,
18 + headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
19 + };
20 + request.get(options, function (error, response, body) {
21 + if (!error && response.statusCode == 200) {
22 + res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'});
23 +
24 +
25 + MovieSearchParse = JSON.parse(body);
26 + //movietitlelist =MovieSearchParse.items[0].title.replace(/<[^>]*>?/g, '');
27 + movietitlelist = MovieSearchParse.items.map((x)=>{
28 + x.title = x.title.replace(/<[^>]*>?/g, '');
29 + return x.title;
30 + });
31 + movielist = MovieSearchParse.items.map((x)=>{
32 + x.title = x.title.replace(/<[^>]*>?/g, '');
33 + return x;
34 + });
35 + console.log(movielist);
36 +
37 + res.end(body);
38 + } else {
39 + res.status(response.statusCode).end();
40 + console.log('error = ' + response.statusCode);
41 + }
42 + });
43 +});
44 +
45 +
46 + app.listen(3000, function () {
47 + console.log('http://127.0.0.1:3000/search/movie?query=검색어 app listening on port 3000!');
48 + });
49 +
1 +{
2 + "dependencies": {
3 + "express": "^4.18.1",
4 + "request": "^2.88.2"
5 + }
6 +}