question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

axios + vercel + servermiddleware issue

See original GitHub issue

hi,

there is no way i can find a good solution to make the api url work well.

my vercel.json: https://github.com/nuxt/now-builder/blob/master/examples/basic/now.json

my nuxt.config.js:

export default {
	serverMiddleware: [
		{
			path: '/api/test',
			handler: '~/server-middleware/test.js'
		}
	]
}

my /server-middleware/test.js:

const json = JSON.stringify({
	status: 'hello'
})
export default (req, res) => {
	res.writeHead(200, {'Content-Type': 'application/json'}).end(json)
}

my /pages/index.vue:

<template>
	<div>
		<p v-if="$fetchState.pending">Fetching posts...</p>
		<p v-else-if="$fetchState.error">Error while fetching posts: {{ $fetchState.error.message }}</p>
		<h1>Works! {{ status }}</h1>
	</div>
</template>

<script>
export default {
	async fetch () {
		this.status = await this.$axios.$get('/api/test')
	},
	data () {
		return {
			status: {}
		}
	}
}
</script>

after deploy, you always want to get it at http://127.0.0.1/api/test.

how can i specify that the vercel url be the api url? what is the good solution for this? i tried process.env.VERCEL_URL too but it didn’t work.

of course api is good in local environment, it works.

thanks for the help

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
zernoniacommented, May 28, 2020

You can easily setup with these config :

VERCEL_URL = "https//example.now.sh"
env: {
    baseUrl: process.env.VERCEL_URL || 'http://127.0.0.1:3000'
  },

then simply

modules: [
    ['@nuxtjs/axios', { baseURL: process.env.baseUrl }],
  ],
1reaction
danielroecommented, Sep 6, 2020

Hopefully this explanation might be helpful.

@kukac7 System environment variables work just fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

axios + vercel + servermiddleware issue #290 - GitHub
hi, there is no way i can find a good solution to make the api url work well. my vercel.json: ...
Read more >
ERROR 500 on axios API Call in Next JS after deployment to ...
SOLUTION: I looked into the Vercel functions logs and got an error for the handlebars template file. File not found.
Read more >
cannot set headers after they are sent to the client axios
I have an issue where if i reload the page im standing on in my nuxt app it redirects me to the login...
Read more >
dtinth/nuxtjs__vercel-builder NPM
A common use case in Nuxt is to use serverMiddleware put an API server on the same server as Nuxt. ... Set up...
Read more >
API Routes: Request Helpers - Next.js
API Routes provide built-in request helpers that parse the incoming request. Learn more about them here.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found