Create API and Testing in Node.js — a practical guide to create Node.js API with clear examples you can reuse in real projects.
Create API and Testing in Node.js — 24/55 in the Node.js series. Full playlist companion: Node.js complete course on YouTube. Prefer one article? Read the complete Node.js tutorial.
Short description
A basic API inspects `req.url` and `req.method`, then returns JSON. Test endpoints with Postman, Thunder Client, curl, or the browser for GET.
Tiny JSON API
const http = require('http');
http.createServer((req, res) => {
if (req.url === '/api/health' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
return res.end(JSON.stringify({ status: 'ok' }));
}
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not found' }));
}).listen(3000);