Giter Club home page Giter Club logo

nuxt-bridge-proxy-sample's Introduction

NuxtBridge Proxy Sample

起動

  • Dockerビルド&起動
docker build . -t nuxt-bridge-proxy-sample && docker run --rm -p 8080:3000 -e NUXT_PUBLIC_MY_DOMAIN='http://localhost:8080' nuxt-bridge-proxy-sample
  • 画面表示 (テストの為、ポート番号を変更)
http://localhost:8080/

Proxy設定

  • http-proxy-middleware で Proxy を行う方針。
  • fromNodeMiddleware で取得できる middleware を使って適用する。
// server-middleware/proxy.ts

import { createProxyMiddleware, fixRequestBody } from 'http-proxy-middleware';

// Proxy先はRuntimeConfigで設定変更できるよう考慮
const { proxyTarget } = useRuntimeConfig();

const proxyMiddleware = createProxyMiddleware({
  target: proxyTarget,
  changeOrigin: true,
  ws: true,
  pathRewrite: {
    '^/api': '/',
  },
  pathFilter: ['/api/**'],
  on: {
    proxyReq: fixRequestBody,
  },
  logger: console,
});

export default fromNodeMiddleware((req, res, next) => {
  proxyMiddleware(req, res, next);
});

API呼び出し

  • useFetch利用時、リクエスト先URLにFQDNを指定しないと、SSR時にエラーが発生する。(恐らく内部の不具合)
  • その為、SSRでは内部ホスト http://localhost:3000/* に、CSR時はドメイン指定を省略 /* に、リクエスト先URLを変更する必要がある。
  • これを簡易にやる為、plugins/apiDomain.ts でリクエスト先のFQDNを取得する処理を用意している。
// plugins/apiDomain.ts
export default defineNuxtPlugin((nuxtApp) => {
  const config = useRuntimeConfig();
  // CSR時にドメインを省略させて $axios.$get すると、何故かNuxtの自ホスト (localhost:3000) にリクエストしてしまう。
  // その為、明示的にRuntimeConfigで公開時の自ドメインを明示的に設定する事にする。(ちょっと煩雑...)
  nuxtApp.provide('apiDomain', () => nuxtApp.ssrContext ? 'http://localhost:3000' : config.public.myDomain);
});
// pages/ditto.vue
<script lang="ts" setup>
const { $axios, $apiDomain } = useNuxtApp();
const data = ref({});

(async () => {
  data.value = await $axios.$get(`${$apiDomain()}/api/v2/pokemon/ditto`);
})();
</script>

nuxt-bridge-proxy-sample's People

Contributors

yuta-mitsuhashi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.