#How to Use Next.js Frontend with Gin/Go Backend/Server?

1 messages · Page 1 of 1 (latest)

ivory delta
#

Question above. I have the following structure for my app:

appDir
   services
      frontend (contains my Next.js app)
      backend (contains my Go/Gin Backend)

How would I connect the two?

compact raftBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

spring reef
#

I would just use something like axios to make requests to your back end. Create a client.js file which initializes the axios client with a base url and any other configuration you need, then in a separate file, create a wrapper function for each endpoint you have which invokes that endpoint using the axios client you've created

Keep in mind, you will end up with two separate servers running: one for your Go/Gin api and one for your next.js server. Any getServerSideProps or getStaticProps code you write will run on the next.js server and will need to be coded in javascript. This may mean you need to add javascript wrappers for your data layer on your next.js server as well if you need to access that data in order to render the page

In this setup, your next.js server and go server would both be accessing your data layer independently - the next.js server just for any data that is needed to render pages, and the go server for any data needed for any of your api endpoints

ivory delta
#

I have been following this tutorial here but getting some errors: https://webapp.io/blog/nextjs-with-golang/

Webapp.io Blog

If you want a scalable, high-performance backend with a fast-loading frontend, this guide is for you. Let's introduce our technologies: Go is a backend language which has been tremendously popular since being launched by Google 10 years ago. It's simple to learn, has powerful concurrency primitives, and can deploy anywhere

#

I'm also looking to use a Go framework, namely Gin, for my web app development. So I'm following this tutorial here: https://gin-gonic.com/docs/quickstart/

#

Oops let me be more specific.
When inside @/services/backend I'm running the following in order to install the Gin framework for Go:

go install github.com/gin-gonic/gin@latest

...and getting the following error:

package github.com/gin-gonic/gin is not a main package
ivory delta
#

Update: Managed to install the Gin framework, however, the last step in the "How to use NextJS with Golang" article I sent is incorrect; specifically, the way to connect the Next frontend with the Golang backend. There is no longer a @/pages/index.js. How do I fix this?

ivory delta
#

Hey @cedar anvil are you online? I was hoping you could help me out with this

river raftBOT
ivory delta
#

Okay my bad I won't do that again

earnest plover
#

@ivory delta

#

wassup

#

i am quite literally your savior xD

#

one sec

#

Thats a nextjs + golang monorepo

#

I am working on a new project rn, you could also check that out
https://github.com/Arinji2/vocab-thing

cmd/api is the golang api
cmd/web is the nextjs website

This repo will also have a chrome and firefox extensions code which will be in cmd/extensions

It also has a sqlite setup with migrations from goose

#

choose whatever you want

ivory delta
#

I will go through these repos as fast as possible

earnest plover
ivory delta
earnest plover
earnest plover
#

@ivory delta any updates?

ivory delta
# earnest plover <@298938485677621280> any updates?

I will let you know if there is any, I had to spend the past two days doing some work as I had just come back from an international trip. I will mark as complete for now and update you if I have any questions.

compact raftBOT
earnest plover
#

Imma unfollow the post.. ping me if you have other questions