How to run both svelte and go

huangapple go评论91阅读模式
英文:

How to run both svelte and go

问题

我正在尝试使用Svelte(前端)和Golang(后端)制作一个网站。
我的问题是,当我在不同的终端中运行它们来测试我的应用程序时(对于Svelte是'npm go dev',对于Golang是'go run .'),它们在不同的端口上运行。Golang在8080端口,Svelte在50838端口。我该如何解决这个问题?

英文:

I'm trying to make a website using svelte(front) and golang(backend).
My problem is when I run those in different terminal to test my app('npm go dev' for svelte, 'go run .' for go), they run in different port. Go in port 8080 and Svelte in port 50838. How can I solve this?

答案1

得分: 1

使用vite to proxy请求到你的Go后端可能是最简单的方法(我假设你正在使用vite!)。

要做到这一点,请在你的vite.config.js中添加以下内容:

const config = {
...,
	server: {
		proxy: {
			'/api': {
				target: 'http://127.0.0.1:8080/',
				proxyTimeout: 10000
			},
            '/': { // 复杂示例,根据头部信息进行过滤
				target: 'http://127.0.0.1:8080/',
				proxyTimeout: 600000,
				bypass: (req, _res, _options) => {
					let ct = req.headers['content-type'];
					if (ct == null || !ct.includes('grpc')) {
						return req.url; // 绕过此代理
					}
				}
			}
		},
	}
};

这里包含了一些示例;你需要根据自己的需求进行调整。

英文:

Using vite to proxy requests to your Go backend is probably the simplest method (I'm assuming you are using vite!).

To do this add something like the following to your vite.config.js:

const config = {
...,
	server: {
		proxy: {
			'/api': {
				target: 'http://127.0.0.1:8080/',
				proxyTimeout: 10000
			},
            '/': { // Complex example that filters based on headers
				target: 'http://127.0.0.1:8080/',
				proxyTimeout: 600000,
				bypass: (req, _res, _options) => {
					let ct = req.headers['content-type'];
					if (ct == null || !ct.includes('grpc')) {
						return req.url; // bypass this proxy
					}
				}
			}
		},
	}
};

This contains a few examples; you will need to tweak these to meet your needs.

huangapple
  • 本文由 发表于 2022年8月19日 08:03:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/73410597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定