# 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 November 19, 2026

By Gregor Dzierzon

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](https://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:

```bash
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:

```bash
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 `/health` route is yours to add.
- `package.json`: its `start` script is yours to fix.
- `.gitignore`: yours to finish.
- `trails.js`, `trail-data.js`, `server.js` and `seed.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.js` checks your work. `trails.test.js` checks 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:

```javascript
//   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" }`.

1. **Finish `loadConfig(env)`** in `config.js`, as its comment says.
2. **Add `GET /health`** to `app.js`: `200` with `{ "status": "ok" }` when `await trails.pingDatabase()` succeeds, and `503` with `{ "status": "unavailable" }` when it throws.
3. **Fix the `start` script** in `package.json`, so it runs `server.js` with neither `--env-file=.env` nor `--watch`. Leave `dev` as it is.
4. **Add a line `.env`** to `.gitignore`, keeping `node_modules/`.
5. **Make `.env.example`**, with a line that starts `MONGODB_URI=` and shows the Atlas connection string with placeholders in angle brackets, such as `<db_password>`, never your real one.

## Check your work

```bash
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_URI` is true for a missing value and for `""`, as *Keeping Secrets Out of Your Code* showed.
- **Environment variables are text.** `Number("10000")` gives `10000`, as *Put Your API Online with Render* reads `PORT`, and `Number("abc")` gives `NaN`. `Number.isInteger(port)` is true only for a whole number.
- **The health route needs `try` and `catch`.** *Put Your API Online with Render* shows one; here `trails.pingDatabase()`, from the repository `createApp` makes, does the asking.
- **The start script is for the host.** On Render there's no `.env` file, and Render supplies `MONGODB_URI` and `PORT` itself.

## Put it online

When every test is green:

1. **Make your Atlas cluster**, as *A Free MongoDB Atlas Cluster* shows: the Free cluster, a database user, and `0.0.0.0/0` in the IP access list.
2. **Copy `.env.example` to `.env`**, and put your own connection string in it. Then put the trails into Atlas, once:

   ```bash
   npm run seed
   ```

   ```text
   Added 6 trails to the trail_conditions database.
   ```

3. **Try it on your computer** with `npm run dev` and `trails.http`: the answers now come from Atlas. Stop it with **Ctrl+C**.
4. **Push your work to a public repository of your own**: steps 1 and 2 of *Submit your exercise*, below.
5. **Make the Render web service** from that repository, as *Put Your API Online with Render* shows: build command `npm ci`, start command `npm start`, the Free instance type, `MONGODB_URI` as an environment variable, and `/health` as the health check path.
6. **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.
7. **Add your live link to `README.md`**, on its `Live link:` line, such as `https://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):

```text
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.

1. 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.
2. 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:

   ```bash
   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 main
   ```

3. Paste 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.

## Key terms

- [process.env](https://www.ask-codey.com/glossary/process-env)
- [Health Check](https://www.ask-codey.com/glossary/health-check-rest-api)
- [MongoDB Atlas](https://www.ask-codey.com/glossary/mongodb-atlas)
- [.env File](https://www.ask-codey.com/glossary/env-file)

## Related

- [A Free MongoDB Atlas Cluster](https://www.ask-codey.com/lessons/node-deploy-atlas-cluster)
- [Put Your API Online with Render](https://www.ask-codey.com/lessons/node-deploy-render)
- [Keeping Secrets Out of Your Code](https://www.ask-codey.com/lessons/node-security-secrets)

Source: https://www.ask-codey.com/lessons/exercise-node-trail-conditions-online
