Home >Web Front-end >JS Tutorial >How does the node server achieve the acquisition of Douban data (code)

How does the node server achieve the acquisition of Douban data (code)

不言
不言Original
2018-08-08 11:00:361793browse

The content of this article is about how the node server implements the acquisition of Douban data (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
    <input type="text" class="ipt" />
    <button>获取数据</button>
    <ul>
 
    </ul>
</body>
<script type="text/javascript">
    
//    $(&#39;button&#39;).click(function(){
//        $.ajax({
//            type:"post",
//            url:"http://localhost:3000",
//            data:{name:$(&#39;.ipt&#39;).val()},
//            success:function(data){
//                console.log(data)
//            }
//        });
//    })
 
 
    $(&#39;button&#39;).click(function(){
        $.ajax({
            type:"get",
            url:"http://localhost:3000",
            dataType:&#39;json&#39;,
            success:function(data){
                console.log(data)
                data.map(function(item){
                    $(&#39;ul&#39;).append(&#39;<li>&#39;+item+&#39;</li>&#39;)
                })
            }
        });
    })
    
    
</script>
</html>
const https=require(&#39;https&#39;);
var http=require(&#39;http&#39;)
const options = {
  hostname: &#39;api.douban.com&#39;,
  port: 443,
  path: &#39;/v2/movie/top250&#39;,
  method: &#39;GET&#39;
};
var str=&#39;&#39;
var arr=[]
//发起请求
const req = https.request(options, (res) => {
 
      res.on(&#39;data&#39;, (d) => {
//        console.log(d.toString())
        str+=d;    
      });
      
      res.on(&#39;end&#39;,()=>{
          var list=JSON.parse(str).subjects
          list.map(function(item){
              arr.push(item.title) 
          })
          
      })
});
 
http.createServer((req,res)=>{
    res.writeHead(200,{&#39;content-type&#39;:&#39;text/html;charset=utf-8&#39;,&#39;Access-Control-Allow-Origin&#39;:&#39;*&#39;})
    if(req.url!=&#39;/favicon.ico&#39;){
        res.write(JSON.stringify(arr))
        res.end()
    }
}).listen(3000)
 
 
//请求失败
req.on(&#39;error&#39;, (e) => {
  console.error(e);
});
//结束请求
req.end();

Recommended related articles:

Communication between Vue child components and parent components (with code)

v-model implementation What is the principle? Introduction to how to use v-model (with code)

The above is the detailed content of How does the node server achieve the acquisition of Douban data (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn