Using Vue.js with Buffalo
When I’m writing web applications, I don’t tend use the latest JavaScript front-end hotness, instead I prefer to reload the entire page on a request, or at the most, a small section of it. Today, however, many developers love to write JSON back ends and write their front-end logic using JavaScript.
In this article, we’re going to do just that. We’re going to create a small Buffalo application that speaks JSON, and we’ll create a small front-end to talk to that using Vue.js. All I ask is your understanding that I’m not a front-end developer, so I’m sure there are plenty of improvements that could be made to my Vue code. Be gentle.
NOTE: To follow along with this article you will need Buffalo installed along with Node/NPM for the front end assets.
The Go Side
The first step on our journey to front-end greatness starts with creating a new Buffalo application. Since Buffalo is Go code, we have to create our project inside our GOPATH
.
|
|
When generating the new Buffalo application we could have used the --api
, which sets the application up to be a JSON API, and doesn’t install any of the front-end services such as templates, JavaScript, CSS, etc… however, since we need those for this particular application we decided to generated a “full” application, instead of a bare bones API application.
We also, using the buffalo db create -a
command, setup all of the databases for this application as defined in the application’s database.yml
.
NOTE: If you are following along at home, you might need to change the settings inside of the
database.yml
to suit your needs.
Generating Resources
Next, let us create some resources for application to talk to. This application will hold information about bands and their members.
|
|
By default when generating resources in Buffalo they are generated as HTML resources, but since this application wants to speak JSON we can use the --type=json
flag to tell the resource generator to use JSON responses, and not create any HTML templates.
Inside of the actions/app.go
we should now have two lines that look like this:
|
|
Let’s create a new api
group and hang these resources off of that. To do that we can create a new group, api
, and hang the BandsResource
off of it. We’re going to also want to nest the MembersResource
under the band
group as well.
|
|
With these changes in place if we were to print off our table, buffalo task routes
, it would look something similar to this:
|
|
Now that we have generated and mapped all of our resources we need to tweak the MembersResource
so that it is scoped to the requested band. We don’t want to show members of the Rolling Stones if someone is requested the members of the Beatles.
For example, in MembersResource#List
we would change the call that finds all of the members to scope it to the band_id
on the request.
|
|
After making these changes in the MembersResource#List
, MembersResource#Create
, MembersResource#Update
, and MembersResource#Destroy
actions we are almost finished with the Go side of the application.
The final step, before we can move the JavaScript side is to set a catch-all route. A catch-all route will allow for the application to accept any URL we haven’t already defined and let the Vue router handle those requests instead.
In the actions/app.go
file we can add this catch-all route right before the route mapping /
to the HomeHandler
.
|
|
With that we are finished with the Go side of the application. Now we can turn our attention to hooking up Vue.js.
The JavaScript Side
To get started on the JavaScript side we first need to install four Node modules, to do this we will use Yarn. These modules will allow us access to Vue, a router for Vue, and a few other pieces like the ability to compile Vue templates.
|
|
With the proper modules installed we need to tell Webpack how to work with these modules. To do that we need to add the following entry to the webpack.config.js
file that Buffalo generates.
|
|
With all the glue in place, and the proper modules installed, we can write our Vue application. Since this article isn’t about learning Vue, and since I’m not a Vue expert, I’m going to simply show you the code I wrote to make my simple application work.
|
|
|
|
|
|
In order to get *.vue
files to work with Webpack, we need to add a rule that tells Webpack to use the vue-loader
plugin to process those files for us. We can update the webpack.config.js
file and add a rule to that affect.
|
|
Putting It All together
With all of that in place we are almost ready to start our application and try it out. We just need one more bit of glue code, and a script to seed the database with a few bands to start with.
Let’s start with the glue code. In order to start the Vue application we need to give it an HTML element to bind to. To do this we can replace the contents of templates/index.html
with the following.
|
|
The above code will not only allow Vue to bind to the page, but it also provides an element for the Vue router to attach to and replace the content of as we navigate pages.
Finally, let’s add a script to seed the database with a few bands. When we generated the application, Buffalo, created a new file, grifts/db.go
. This file contains a db:seed
task. The purpose of this task is to let us write a seed script for our database.
We can replace the placeholder of this script with the following:
|
|
This script can be run like such:
|
|
Buffalo uses the grift task runner for these types of simple, repeatable scripts.
With seed data in place we can launch the application in development mode.
|
|
The buffalo dev
command will not only start the application at http://localhost:3000
, it also watches your Go files, and assets, for changes. If there are changes then Buffalo will recompile your application, and/or assets, and restart it.
Conclusion
In this article we built a brand new, database backed, Buffalo application that speaks JSON. We also quickly built a single page application on top of the Buffalo application, thanks to Buffalo’s Webpack asset pipeline.
Hopefully this article has inspired you to try your favorite framework on top of Buffalo. Perhaps using GopherJS. :)
Full source code can be found at https://github.com/gobuffalo/vuerecipe.
About the Author
Mark is the co-founder of PaperCall.io, a platform for connecting technical events with high quality content and speakers. Mark is also a partner at Gopher Guides, the industry leader for Go training and conferences. In his spare time Mark leads development of the Go web framework Buffalo.