英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论