new examples

This commit is contained in:
Andras Bacsai
2025-12-26 11:40:00 +01:00
parent 35e4d33085
commit d3a2a9d83b
1164 changed files with 101362 additions and 160055 deletions

View File

@@ -0,0 +1 @@
node_modules/

1
node/simple-webserver/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
node_modules/

View File

@@ -0,0 +1,14 @@
# Simple Webserver
A simple HTTP server using pure Node.js (no dependencies).
## Getting Started
```bash
npm start
```
## Endpoints
- `GET /` - Returns `{"message": "Hello from Node.js!"}`
- `GET /health` - Returns `{"status": "ok"}`

View File

@@ -0,0 +1,32 @@
const http = require('http');
const PORT = process.env.PORT || 3000;
// Runtime env vars (read at server startup)
const RUNTIME_PRIVATE_VAR = process.env.RUNTIME_PRIVATE_VAR || 'default-value';
const RUNTIME_PUBLIC_VAR = process.env.RUNTIME_PUBLIC_VAR || 'default-value';
console.log('=== Runtime Variables ===');
console.log('RUNTIME_PRIVATE_VAR:', RUNTIME_PRIVATE_VAR);
console.log('RUNTIME_PUBLIC_VAR:', RUNTIME_PUBLIC_VAR);
const server = http.createServer((req, res) => {
if (req.url === '/' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
message: 'Hello from Node.js!',
runtimePrivateVar: RUNTIME_PRIVATE_VAR,
runtimePublicVar: RUNTIME_PUBLIC_VAR,
}));
} else if (req.url === '/health' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'ok' }));
} else {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not found' }));
}
});
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});

View File

@@ -0,0 +1,11 @@
{
"name": "simple-webserver",
"version": "1.0.0",
"description": "A simple backend webserver written in nodejs",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"keywords": [],
"license": "MIT"
}