Lesson 12 · Node.js on IBM i — From RPG Developer to Modern IBM i Developer
Create the Tools Rental API
Add Fastify, expose health and tools endpoints, and run the first HTTP service from IBM i.
The database module already answers useful questions. An HTTP API gives a browser or another application a controlled way to ask them.
We will use Fastify for routing, JSON responses, validation, and structured logging. The project still owns the business rules and SQL; the framework handles the HTTP plumbing.
Install the web framework
From the project directory on IBM i:
npm install fastify
Commit the updated package.json and package-lock.json. Do not copy node_modules between systems.
Create the server
Create src/server.js:
const Fastify = require('fastify');
const {
findToolById,
listAvailableTools
} = require('./db');
function buildServer() {
const server = Fastify({ logger: true });
server.get('/health', async () => {
return { status: 'ok' };
});
server.get('/tools', async () => {
return listAvailableTools();
});
server.get('/tools/:toolId', {
schema: {
params: {
type: 'object',
required: ['toolId'],
properties: {
toolId: { type: 'integer', minimum: 1 }
}
}
}
}, async (request, reply) => {
const tool = await findToolById(request.params.toolId);
if (!tool) {
return reply.code(404).send({ message: 'Tool not found' });
}
return tool;
});
return server;
}
async function start() {
const server = buildServer();
const port = Number(process.env.PORT ?? 3000);
const host = process.env.HOST ?? '127.0.0.1';
try {
await server.listen({ port, host });
} catch (error) {
server.log.error(error);
process.exitCode = 1;
}
}
if (require.main === module) {
start();
}
module.exports = { buildServer };
Exporting buildServer will let our tests create the application without opening a real network port.
Update the npm scripts
Change the scripts in package.json:
"scripts": {
"start": "node src/server.js",
"dev": "node --watch src/server.js"
}
The server replaces the small src/app.js program from the earlier lessons. Remove it so the project has one entry point:
git rm src/app.js
Start the API:
npm start
From another SSH session on the same IBM i, try the routes with curl. If the shell cannot find it, ask your administrator to install it with yum install curl.
curl http://127.0.0.1:3000/health
curl http://127.0.0.1:3000/tools
curl http://127.0.0.1:3000/tools/1
The health request should return:
{"status":"ok"}
Bind deliberately
The default host is 127.0.0.1, so only the local system can reach the service. That is a safe starting point.
Setting HOST=0.0.0.0 makes the process listen on available network interfaces, but it does not by itself make the application production-ready. Network rules, TLS, authentication, reverse-proxy configuration, and IBM i security still need deliberate decisions.
Keep route handlers small
The route receives an HTTP request, calls a project function, and chooses an HTTP response. It does not contain SQL. As the application grows, that separation will keep database code testable and prevent every route from inventing its own connection handling.
Commit this checkpoint:
git add package.json package-lock.json src
git commit -m "Add tools rental API"
The API can read tools but cannot change anything yet. The next lessons add the rental and return workflows with the database protecting each business transaction.
References
Documentation and references used for this lesson.

Comments
Share your thoughts, questions, or real-world IBM i experiences related to this article.