Setting Up An API Testing Server With Ubuntu 16.04, Vagrant, VirtualBox, NodeJs and FeathersJs - Morphatic
Setting Up An API Testing Server With Ubuntu 16.04, Vagrant, VirtualBox, NodeJs and FeathersJs - Morphatic
04,
Vagrant, VirtualBox, NodeJs
and FeathersJs
Recently, I’ve been using Feathers to generate REST and
realtime data APIs for my Angular(2+) and Ionic(2+) apps.
During development, it’s useful to have a local virtual
machine that’s running the API. It’s pretty straightforward to
swap out the live URL for the API server with the local dev
API URL so that I don’t have to pollute my actual API server
with dummy data and/or prior to launching the actual API
server, I can run a local instance. The current versions of the
tools that I’m using are: Feathers–An open
source REST and
realtime API layer for
Vagrant 2.0.1 modern applications.
VirtualBox 5.2.0
Ubuntu 16.04 LTS
NodeJS 9.x
Feathers CLI 2.3.9
MongoDB 3.6 (beta)
Many of these steps are identical or updates of steps from this older tutorial. Here
are the steps to set this up:
Next, grab the latest version of Vagrant. Again, you can just run the installer to
overwrite any previously installed version with the latest one.
Next, in your favorite text editor, you need to update the Vagrantfile (which was
created for you in your current folder) so that it has enough memory. In my case, I
also configured a “private network” IP address and a synced folder. Here’s what
my Vagrantfile looks like with everything but the necessary bits removed:
Vagrantfile for my Ubuntu 16.04 Strongloop box Ruby
1 Vagrant.configure("2") do |config|
2 config.vm.box = "ubuntu/xenial64"
3 config.vm.network "private_network", ip: "192.168.33.10"
4 config.vm.synced_folder "./api", "/home/ubuntu/api"
5 config.vm.provider "virtualbox" do |vb|
6 vb.memory = "2048"
7 end
8 end
Once you save this file, you can go back to the terminal, manually create
the api folder, and fire up your box:
1 $ mkdir api
2 $ vagrant up --provider virtualbox
NOTE: I found that on one of my machines I had trouble with this command.
It appears that for some systems the version of curl that ships with Vagrant
doesn’t work. Follow the instructions on this thread to figure out how to
replace it with one that works!
NOTE: You don’t have to use VirtualBox for hosting Vagrant boxes. You can
also use another provider like VMWare if you know that platform better and
are more comfortable with it. I prefer VirtualBox because it is FOSS.
Alternatively, if you’d like to install other versions of Node (like the current, non-
LTS version) you can find the instructions on the NodeSource repo site.
Step 5: Tweak the NodeJS Installation
In order for npm to be able to install everything in /usr/local you need to change
its ownership to the default user (not root). You also need to make sure that npm is
configured to install packages to /usr/local which, I was surprised to find, was not
the default. (In my case the default prefix was /usr .)
$ sudo chown -R $USER:$USER /usr/local
$ npm config set prefix /usr/local
NOTE: If you are on Windows, you’ll also want to set the following
configuration flag for Node because for some reason, when running Node on
Ubuntu using Vagrant/VirtualBox on Windows, it doesn’t like you to use
symlinks:
$ npm config set bin-links false
Paste the following code into the file and then hit CTRL + x to exit, y to save and
then enter to get back to the command line:
Shell
1 [Unit]
2 Description=High-performance, schema-free document-oriented database
3 After=network.target
4
5 [Service]
6 User=mongodb
7 ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
8
9 [Install]
10 WantedBy=multi-user.target
It is not necessary to adjust the firewall since we’ll only be accessing MongoDB
locally from our REST API server.
Of course you can substitute any URL that you’d like here, but be careful not to use
an actual URL that you really want to visit on the web because it will block your
computer from having access to it.
Follow the prompts and answer the questions as they come up. The defaults are
probably fine for most of the questions, although I’d recommend coming up with a
more unique app name, and also a good, short description.
Second, I like to use port 80 for my API servers, just for the cleaner URLs. To do
this with Feathers, modify the default configuration in config/default.json to look
like this:
Default config for a Feathers app running on port 80 JavaScript
1 {
2 "host": "localhost",
3 "port": 80,
4 "public": "../public/",
5 "paginate": {
6 "default": 10,
7 "max": 50
8 }
9 }
The only thing you should have to change is the port number. You can either edit
this from the virtual machine using a terminal-based editor like nano or you can
edit it in the host OS in your favorite editor. The file should be
at /path/to/your/feathers/api/config/default.json .
And then navigate to, e.g., http://api.myapp.com.dev in your browser. If all has
gone well you should see the following:
Conclusion
Hopefully at this point you’ve got a foundation for creating your REST/realtime API
service. In my next tutorial, I plan to show you how to configure Feathers to work
with mongoose and MongoDB and also how to consume your new API from an
Angular/Ionic app. Stay tuned!