Exercise: Trail Conditions Online
Get Willow Creek Parks' finished trails API ready for a host (settings read from the environment, a /health route, a start script with no .env file, and the secret kept out of Git), then put it online with a free Atlas cluster and Render, and hand in a repository whose README links the live API.
Journeyman · Node.js · MongoDB · Web Development · Updated
Hikers keep phoning Willow Creek Parks' ranger station to ask which trails are muddy. The parks department's trails API is finished and tested, but it only runs on a laptop. You'll get it ready for a host, the way this chapter showed, and then put it online with a free Atlas cluster and Render, so the link you hand in works on any hiker's phone.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-node-trail-conditions-online, also linked from the Exercise box beside the lesson. Clone it into your nodejs-course folder. In VS Code's terminal:
cd ~/nodejs-course
git clone https://github.com/ask-codey/exercise-node-trail-conditions-online.git
Open the new exercise-node-trail-conditions-online folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and install the packages:
npm install
Get the Exercise Code with Git walks through cloning step by step, if you'd like a reminder.
The folder holds:
config.js:loadConfig(env), which reads the API's settings. For you to finish: today it only works on a laptop.app.js:createApp(db), which builds the Express app around the database it's given, as in Wiring MongoDB into Express. The trail routes are finished; the/healthroute is yours to add.package.json: itsstartscript is yours to fix..gitignore: yours to finish.trails.js,trail-data.js,server.jsandseed.js: the trails in MongoDB, the six starting trails, the server, and a script that puts the trails into your Atlas cluster. Ready to use.trails.http: the requests to try with REST Client, on your computer and online.ready-for-a-host.test.jschecks your work.trails.test.jschecks the finished trail routes, so it passes from the start. You don't need to change either.
What to do
config.js and app.js each start with a comment listing exactly what they must do. Here is config.js's:
// PORT port: the number PORT holds, such as 10000 for "10000" (a number, not text).
// When PORT isn't set, or is empty, port is 3100.
// When PORT isn't a whole number, such as "abc", throw an Error with the message
// PORT must be a whole number, such as 3100.
// MONGODB_URI mongodbUri: the connection string, exactly as it was given.
// When MONGODB_URI isn't set, or is empty, throw an Error with the message
// MONGODB_URI is missing: set it in .env on your computer, or in your host's environment variables.
For example, loadConfig({ PORT: "10000", MONGODB_URI: "mongodb://localhost:27017/trails-test" }) gives back { port: 10000, mongodbUri: "mongodb://localhost:27017/trails-test" }.
- Finish
loadConfig(env)inconfig.js, as its comment says. - Add
GET /healthtoapp.js:200with{ "status": "ok" }whenawait trails.pingDatabase()succeeds, and503with{ "status": "unavailable" }when it throws. - Fix the
startscript inpackage.json, so it runsserver.jswith neither--env-file=.envnor--watch. Leavedevas it is. - Add a line
.envto.gitignore, keepingnode_modules/. - Make
.env.example, with a line that startsMONGODB_URI=and shows the Atlas connection string with placeholders in angle brackets, such as<db_password>, never your real one.
Check your work
npm test
No server needs to be running, and you don't need .env: the tests start a throwaway MongoDB of their own. At first the 11 tests in ready-for-a-host.test.js fail, and the 5 trail-route tests pass. Each step above turns its tests green. A failing test's message says what it expected, for example GET /health should answer 200, but it answered 404.
Hints
- Empty counts as missing.
!env.MONGODB_URIis true for a missing value and for"", as Keeping Secrets Out of Your Code showed. - Environment variables are text.
Number("10000")gives10000, as Put Your API Online with Render readsPORT, andNumber("abc")givesNaN.Number.isInteger(port)is true only for a whole number. - The health route needs
tryandcatch. Put Your API Online with Render shows one; heretrails.pingDatabase(), from the repositorycreateAppmakes, does the asking. - The start script is for the host. On Render there's no
.envfile, and Render suppliesMONGODB_URIandPORTitself.
Put it online
When every test is green:
Make your Atlas cluster, as A Free MongoDB Atlas Cluster shows: the Free cluster, a database user, and
0.0.0.0/0in the IP access list.Copy
.env.exampleto.env, and put your own connection string in it. Then put the trails into Atlas, once:npm run seedAdded 6 trails to the trail_conditions database.Try it on your computer with
npm run devandtrails.http: the answers now come from Atlas. Stop it with Ctrl+C.Push your work to a public repository of your own: steps 1 and 2 of Submit your exercise, below.
Make the Render web service from that repository, as Put Your API Online with Render shows: build command
npm ci, start commandnpm start, the Free instance type,MONGODB_URIas an environment variable, and/healthas the health check path.Try the live API. In
trails.http, put your Render address in@baseUrl, and send the requests again. If the service has been asleep, the first answer takes about a minute.Add your live link to
README.md, on itsLive link:line, such ashttps://your-service-name.onrender.com/trails. Commit and push: Render deploys again by itself.
When you're done
Every test is green, and your API gives these answers, on your computer and at your live link (each request, then its answer's status line and body):
GET /health
HTTP/1.1 200 OK
{"status":"ok"}
GET /trails?status=muddy
HTTP/1.1 200 OK
[{"slug":"lookout-ridge","name":"Lookout Ridge Trail","miles":5.8,"status":"muddy","lastReport":{"date":"2026-11-15","note":"Muddy past the second bridge. Wear boots."}},{"slug":"meadow-view-loop","name":"Meadow View Loop","miles":2.4,"status":"muddy","lastReport":{"date":"2026-11-16","note":"Soft ground in the low meadow after rain."}}]
Look at your repository on GitHub: .env.example is there, and .env isn't. Anyone can read your code and run it with their own Atlas cluster, and nobody can read your password.
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.
On GitHub, click +, then New repository. Name it (for example
exercise-node-trail-conditions-online), choose Public, and click Create repository. Leave out the README: your folder already has one.In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-node-trail-conditions-online.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-node-trail-conditions-online, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete. Its README's live link is how anyone reviewing it opens your API.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
The same few changes (settings from the environment, a health route, a start script for the host, and a secret kept out of Git) make any Node API ready to go online, and now you have a live link to prove it.