Skip to content

Commit

Permalink
Successfully reciveing the fetched data from Node.js on Next.js's ser…
Browse files Browse the repository at this point in the history
…ver #12
  • Loading branch information
Jood80 committed Nov 28, 2020
1 parent c509b17 commit a64d52c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
17 changes: 13 additions & 4 deletions pages/news.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import React from 'react'
import Layout from '../src/Layout'
import { fetchedData } from '../utils'

const News = ({ news }) => (
<Layout description="watch your words" keywards="tech">
<line>{news}</line>
const News = ({ news, seo }) => (
<Layout description={seo.description} keywards={seo.keywards}>
<section>
{news.map((newsData) => (
<>
<ul>
<li key={newsData.id}>{newsData.description}</li>
</ul>
</>
))}
</section>
</Layout>
)
export default News
Expand All @@ -13,7 +21,8 @@ export async function getServerSideProps() {
const news = await fetchedData('news')
return {
props: {
news,
news: news.response.sources,
seo: news.seo,
},
}
}
18 changes: 10 additions & 8 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import axios from 'axios'

const backUrl = process.env.BACK_URL
const backURL = process.env.NEXT_PUBLIC_BACK_URL

async function fetchedData(path, params = null) {
let url
if (params !== null) {
url = `${backUrl}/${path}/${params}`
url = `${backURL}/${path}/${params}`
} else {
url = `${backUrl}/${path}`
url = `${backURL}/${path}`
}
try {
const { data: response } = await axios.get(`${url}`)
return response
} catch (err) {
console.log(`Error is ${err}`, err)
}

const response = await axios(`${url}`)
const data = await response.json()
return data
}
export { backUrl, fetchedData }
export { backURL, fetchedData }

0 comments on commit a64d52c

Please sign in to comment.