一個(gè)簡(jiǎn)單的頭條新聞響應(yīng)式(調(diào)用聚合數(shù)據(jù)api)

聚合數(shù)據(jù)API申請(qǐng)鏈接:https://www.juhe.cn/docs/api/id/235

架在服務(wù)器上热芹。 地址可訪問:http://106.13.193.149:8081/

后端使用的是nodejs(express+superagent)

直接上代碼:

文件目錄:

**app.js**

```

//superagent是一個(gè)HTTP庫

這里之所以用superagent舷暮,也是因?yàn)榍岸隧撁嫒绻苯拥艟酆蠑?shù)據(jù)api 會(huì)出現(xiàn)跨域。

const superagent= require('superagent');

var express = require('express');

var app = express();

//解決跨域

app.all('*', function (req, res, next) {

? ? res.header('Access-Control-Allow-Origin', '*');

? ? //Access-Control-Allow-Headers ,可根據(jù)瀏覽器的F12查看,把對(duì)應(yīng)的粘貼在這里就行

? ? res.header('Access-Control-Allow-Headers', 'Content-Type');

? ? res.header('Access-Control-Allow-Methods', '*');

? ? res.header('Content-Type', 'application/json;charset=utf-8');

? ? next();

? });

//靜態(tài)資源路徑

app.use('/public', express.static('public'));

//首頁

app.get('/', function (req, res) {

? ? res.sendFile( __dirname + "/" + "toutiao.html" );

})

//首頁會(huì)調(diào)用此接口狈网,攜帶參數(shù)type 可返回不同的新聞?lì)愋?/p>

app.get('/get_toutiao',(req,res)=>{

? console.log(req.query.type)

? superagent.get(`http://v.juhe.cn/toutiao/index?key=58ff2000704bbf8e31ce031c1b555a33&type=${req.query.type}`).end((err, reso) => {

? ? // console.log(reso);

? ? //返回的是字符,需要前端頁面轉(zhuǎn)換為對(duì)象

? ? res.jsonp({

? ? ? data:reso

? ? })

? });

})

var server = app.listen(8081, function () {

? var host = server.address().address

? var port = server.address().port

? console.log("應(yīng)用實(shí)例斤贰,訪問地址為 http://%s:%s", host, port)

})

```

**toutiao.html**

```

<!DOCTYPE html>

<html>

<head>

? <meta charset="UTF-8">

? <!-- 引入樣式 -->

? <link rel="stylesheet" >

? <link rel="stylesheet" href="./public/toutiao.css">

<title>每日頭條</title>

</head>

<body>

? <div id="app">

? ? <div class="content">

? ? ? ? <div class="head">

? ? ? ? ? ? <div :class="{'dynamic':mark == 'top'}" class="item" @click="get_toutiao('top')">推薦</div>

? ? ? ? ? ? <div :class="{'dynamic':mark == 'shehui'}" class="item" @click="get_toutiao('shehui')">社會(huì)</div>

? ? ? ? ? ? <div :class="{'dynamic':mark == 'guonei'}" class="item" @click="get_toutiao('guonei')">國內(nèi)</div>

? ? ? ? ? ? <div :class="{'dynamic':mark == 'guoji'}" class="item" @click="get_toutiao('guoji')">國際</div>

? ? ? ? ? ? <div :class="{'dynamic':mark == 'yule'}" class="item" @click="get_toutiao('yule')">娛樂</div>

? ? ? ? ? ? <div :class="{'dynamic':mark == 'tiyu'}" class="item" @click="get_toutiao('tiyu')">體育</div>

? ? ? ? ? ? <div :class="{'dynamic':mark == 'junshi'}" class="item" @click="get_toutiao('junshi')">軍事</div>

? ? ? ? ? ? <div :class="{'dynamic':mark == 'keji'}" class="item" @click="get_toutiao('keji')">科技</div>

? ? ? ? ? ? <div :class="{'dynamic':mark == 'caijing'}" class="item" @click="get_toutiao('caijing')">財(cái)經(jīng)</div>

? ? ? ? ? ? <div :class="{'dynamic':mark == 'shishang'}" class="item" @click="get_toutiao('shishang')">時(shí)尚</div>

? ? ? ? </div>

? ? ? ? <div class="body">

? ? ? ? ? ? <div class="item" v-for="item in items">

? ? ? ? ? ? ? ? <div class="title" :class="{left:true}"><a style="font-size:22px" :href="item.url" target="view_window">{{item.title}}</a></div>

? ? ? ? ? ? ? ? <div class="imgs" >

? ? ? ? ? ? ? ? ? ? ![](item.thumbnail_pic_s)

? ? ? ? ? ? ? ? ? ? ![](item.thumbnail_pic_s02)

? ? ? ? ? ? ? ? ? ? ![](item.thumbnail_pic_s03)

? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? <div class="date"><span style="color:#F56C6C;padding-right: 15px;">{{item.author_name}}</span><span>{{item.date}}</span></div>

? ? ? ? ? ? </div>

? ? ? ? ? ? <div class="bottom">

? ? ? ? ? ? ? ? <span>已經(jīng)到底了</span>

? ? ? ? ? ? </div>

? ? ? ? </div>

? ? </div>

? </div>

</body>

? <!-- 先引入 Vue -->

? <script src="https://unpkg.com/vue/dist/vue.js"></script>

? <!-- 引入組件庫 -->

? <script src="https://unpkg.com/element-ui/lib/index.js"></script>

? <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

? <script>

? ? new Vue({

? ? ? el: '#app',

? ? ? data: function() {

? ? ? ? return {

? ? ? ? ? ? items: [],

? ? ? ? ? ? mark:'top'

? ? ? ? }

? ? ? },

? ? ? methods:{

? ? ? ? get_toutiao(type) {

? ? ? ? ? ? this.mark = type;

? ? ? ? ? ? axios.get('/get_toutiao', {

? ? ? ? ? ? ? ? params: {

? ? ? ? ? ? ? ? type: type

? ? ? ? ? ? ? ? }

? ? ? ? ? ? })

? ? ? ? ? ? .then((res)=>{

? ? ? ? ? ? ? ? var list = JSON.parse(res.data.data.text);

? ? ? ? ? ? ? ? console.log(list.result.data)

? ? ? ? ? ? ? ? this.items = list.result.data;

? ? ? ? ? ? })

? ? ? ? ? ? .catch((err)=>{

? ? ? ? ? ? ? ? console.log(err)

? ? ? ? ? ? });

? ? ? ? },

? ? ? ? scrollToTop () {

? ? ? ? ? ? const that = this

? ? ? ? ? ? let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop

? ? ? ? ? ? that.scrollTop = scrollTop

? ? ? ? ? ? if (that.scrollTop > 60) {

? ? ? ? ? ? that.btnFlag = true

? ? ? ? ? ? } else {

? ? ? ? ? ? that.btnFlag = false

? ? ? ? ? ? }

? ? ? ? }

? ? ? },

? ? ? mounted() {

? ? ? ? ? this.get_toutiao();

? ? ? ? ? window.addEventListener('scroll', this.scrollToTop)

? ? ? }

? ? })

? </script>

</html>

```

**public/toutiao.css**

```

body{

? ? margin: 0;

? ? padding: 0;

? ? color: black

}

a{text-decoration:none}

a:link {color: #000} /* 未訪問時(shí)的狀態(tài) */

a:visited {color: #000} /* 已訪問過的狀態(tài) */

a:hover {color: #F56C6C;text-decoration:underline} /* 鼠標(biāo)移動(dòng)到鏈接上時(shí)的狀態(tài) */

a:active {color: #F56C6C} /* 鼠標(biāo)按下去時(shí)的狀態(tài) */

.content{

? ? width: 100%;

? ? /* height: 100vh; */

? ? margin: auto;

? ? padding-top: 75px;

}

.head{

? ? width: 100%;

? ? height: 50px;

? ? position: fixed;

? ? top: 0;

? ? z-index: 100;

? ? display: flex;

? ? justify-content: space-between;

? ? align-items: center;

? ? background-color: #409EFF;

? ? font-size: 18px

}

.head >.item{

? ? width: 8%;

? ? height: 100%;

? ? display: flex;

? ? justify-content: center;

? ? align-items: center;

? ? cursor: pointer;? ?

}

.dynamic{

? ? background-color: #F56C6C;

? ? color:#ffffff

}

.head >.item:hover{

? ? color: #F56C6C

}

.body{

? ? /* margin-top: 105px; */

? ? width: 65%;

? ? margin: auto;

? ? display: flex;

? ? flex-direction: column;

? ? justify-content: flex-start;

? ? align-items: center

}

.body>.item{

? ? width: 100%;

? ? /* height: 150px; */

? ? border-bottom: 1px solid #DCDFE6;

? ? display: flex;

? ? flex-direction: column;

? ? justify-content: space-between;

? ? margin-top: 25px;

}

.body >.item>.imgs{

? ? width: 100%;

? ? display: flex;

? ? justify-content: space-between;

? ? align-items: center;

? ? margin-top: 10px;

? ? margin-bottom: 7px;

}

.body>.bottom{

? ? width: 100%;

? ? height: 152px;

? ? display: flex;

? ? justify-content: center;

? ? align-items: center

}

```

### 效果圖

————————————————

版權(quán)聲明:本文為CSDN博主「BYh_blog」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明瑞驱。

原文鏈接:https://blog.csdn.net/byhage1/article/details/99734445

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市窄坦,隨后出現(xiàn)的幾起案子唤反,更是在濱河造成了極大的恐慌,老刑警劉巖鸭津,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件彤侍,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡逆趋,警方通過查閱死者的電腦和手機(jī)盏阶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來闻书,“玉大人名斟,你說我怎么就攤上這事』菡” “怎么了蒸眠?”我有些...
    開封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長杆融。 經(jīng)常有香客問我楞卡,道長,這世上最難降的妖魔是什么脾歇? 我笑而不...
    開封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任蒋腮,我火速辦了婚禮,結(jié)果婚禮上藕各,老公的妹妹穿的比我還像新娘池摧。我一直安慰自己,他們只是感情好激况,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開白布作彤。 她就那樣靜靜地躺著,像睡著了一般乌逐。 火紅的嫁衣襯著肌膚如雪竭讳。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天浙踢,我揣著相機(jī)與錄音绢慢,去河邊找鬼。 笑死洛波,一個(gè)胖子當(dāng)著我的面吹牛胰舆,可吹牛的內(nèi)容都是我干的骚露。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼缚窿,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼棘幸!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起滨攻,我...
    開封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤够话,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后光绕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體女嘲,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年诞帐,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了欣尼。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡停蕉,死狀恐怖愕鼓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情慧起,我是刑警寧澤菇晃,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站蚓挤,受9級(jí)特大地震影響磺送,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜灿意,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一估灿、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧缤剧,春花似錦馅袁、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至抵窒,卻和暖如春弛针,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背估脆。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來泰國打工钦奋, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留座云,地道東北人疙赠。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓付材,卻偏偏與公主長得像,于是被迫代替她去往敵國和親圃阳。 傳聞我的和親對(duì)象是個(gè)殘疾皇子厌衔,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容