Deploying the API
Deploying the API means putting the server on a machine that stays on. The machine runs the start script and answers requests from the web.
A health route
The route answers with a short JSON message.
app.get('/api/health', (req, res) => {
res.json({ status: 'ok' });
});Starting on the host
Two commands install the code and start the API.
npm install
npm startExample
import express from 'express';
const app = express();
app.get('/api/health', (req, res) => {
res.json({ status: 'ok' });
});
const server = app.listen(0, async () => {
const port = server.address().port;
const response = await fetch('http://localhost:' + port + '/api/health');
console.log(await response.text());
server.close();
});Key points
- A host is a machine that runs the app day and night.
- The host needs Node and the app code, nothing more.
- The port comes from the host settings, not from the code.
- A health route is a small route that answers that the API is alive.
In this example, the script starts the API with a health route. It then fetches the health route like a host would. The printed JSON shows the API is alive.
Note
The database must allow the host to connect.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What is a health route for?