← Каталог
Первая программа на Node.js — Шаг 6 — HTTP-сервер на встроенном `http`
Фрагмент из «Первая программа на Node.js»: Шаг 6 — HTTP-сервер на встроенном `http`.
import http from 'node:http';
const PORT = process.env.PORT || 3000;
const server = http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(JSON.stringify({ status: 'ok' }));
return;
}
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Not found\n');
});
server.listen(PORT, () => {
console.log(`Raw API: http://127.0.0.1:${PORT}`);
}); import http from 'node:http';
const PORT = process.env.PORT || 3000;
const server = http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(JSON.stringify({ status: 'ok' }));
return;
}
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Not found\n');
});
server.listen(PORT, () => {
console.log(`Raw API: http://127.0.0.1:${PORT}`);
});