Bläddra i källkod

npm i @nuxtjs/proxy > proxy 설정 적용

yongun 5 år sedan
förälder
incheckning
f574c92cae
2 ändrade filer med 11 tillägg och 3 borttagningar
  1. 10 2
      nuxt.config.js
  2. 1 1
      pages/index.vue

+ 10 - 2
nuxt.config.js

@@ -46,14 +46,22 @@ export default {
   */
   modules: [
     // Doc: https://axios.nuxtjs.org/usage
-    '@nuxtjs/axios'
+    '@nuxtjs/axios',
+    '@nuxtjs/proxy'
   ],
   /*
   ** Axios module configuration
   ** See https://axios.nuxtjs.org/options
   */
   axios: {
-    credentials: true
+    credentials: true,
+    proxy: true
+  },
+  proxy: {
+    '/api/': {
+      target: 'https://newsapi.org/v2/',
+      pathRewrite: { '^/api/': '' }
+    }
   },
   env: {
     NEWS_API_KEY: '2d199fb5095b44869cb4311a7a0366d2'

+ 1 - 1
pages/index.vue

@@ -10,7 +10,7 @@
 <script>
 export default {
   async asyncData ({ app }) {
-    const topHeadlines = await app.$axios.$get('https://newsapi.org/v2/top-headlines?country=us')
+    const topHeadlines = await app.$axios.$get('/api/top-headlines?country=us')
     return { headlines: topHeadlines.articles }
   }
 }