104 lines
3.6 KiB
JavaScript
104 lines
3.6 KiB
JavaScript
curl http://192.168.1.122:3002const http = require('http');
|
||
const os = require('os');
|
||
|
||
function getLocalIp() {
|
||
const interfaces = os.networkInterfaces();
|
||
for (const name of Object.keys(interfaces)) {
|
||
for (const iface of interfaces[name]) {
|
||
const { address, family, internal } = iface;
|
||
if (family === 'IPv4' && !internal) {
|
||
return address;
|
||
}
|
||
}
|
||
}
|
||
return 'localhost';
|
||
}
|
||
|
||
const hostname = '0.0.0.0';
|
||
const port = 3002;
|
||
const localIp = getLocalIp();
|
||
|
||
const server = http.createServer((req, res) => {
|
||
console.log(`📨 Requête reçue de: ${req.socket.remoteAddress}:${req.socket.remotePort}`);
|
||
console.log(` URL: ${req.url}`);
|
||
console.log(` Host header: ${req.headers.host}`);
|
||
|
||
res.statusCode = 200;
|
||
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
||
res.end(`
|
||
<!DOCTYPE html>
|
||
<html>
|
||
<head>
|
||
<title>Test Serveur</title>
|
||
<meta charset="utf-8">
|
||
<style>
|
||
body {
|
||
font-family: system-ui, sans-serif;
|
||
max-width: 600px;
|
||
margin: 50px auto;
|
||
padding: 20px;
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: white;
|
||
}
|
||
.card {
|
||
background: rgba(255,255,255,0.95);
|
||
color: #333;
|
||
padding: 30px;
|
||
border-radius: 15px;
|
||
box-shadow: 0 10px 40px rgba(0,0,0,0.3);
|
||
}
|
||
h1 { color: #667eea; margin-top: 0; }
|
||
.success { color: #22c55e; font-size: 48px; }
|
||
.info { background: #f0f9ff; padding: 15px; border-radius: 8px; margin: 15px 0; }
|
||
code { background: #e5e7eb; padding: 2px 6px; border-radius: 4px; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="card">
|
||
<div class="success">✅</div>
|
||
<h1>Serveur accessible !</h1>
|
||
<p><strong>🎉 Félicitations !</strong> Si vous voyez cette page, le serveur fonctionne correctement.</p>
|
||
|
||
<div class="info">
|
||
<p><strong>Informations de connexion :</strong></p>
|
||
<ul>
|
||
<li>Adresse IP locale : <code>${localIp}</code></li>
|
||
<li>Port : <code>${port}</code></li>
|
||
<li>Votre IP client : <code>${req.socket.remoteAddress}</code></li>
|
||
<li>Host demandé : <code>${req.headers.host}</code></li>
|
||
</ul>
|
||
</div>
|
||
|
||
<p><strong>URLs d'accès :</strong></p>
|
||
<ul>
|
||
<li>Sur ce Mac : <a href="http://localhost:${port}">http://localhost:${port}</a></li>
|
||
<li>Sur le réseau (IP) : <a href="http://${localIp}:${port}">http://${localIp}:${port}</a></li>
|
||
<li>Sur le réseau (.local) : <a href="http://${os.hostname()}.local:${port}">http://${os.hostname()}.local:${port}</a></li>
|
||
</ul>
|
||
</div>
|
||
</body>
|
||
</html>
|
||
`);
|
||
});
|
||
|
||
server.listen(port, hostname, () => {
|
||
console.log('\n🚀 ========================================');
|
||
console.log(' SERVEUR DE TEST DÉMARRÉ');
|
||
console.log('========================================\n');
|
||
console.log(` ✅ Le serveur écoute sur toutes les interfaces (${hostname}:${port})\n`);
|
||
console.log('📱 TESTEZ CES URLs DEPUIS VOTRE MOBILE :\n');
|
||
console.log(` 1️⃣ http://${localIp}:${port}`);
|
||
console.log(` 2️⃣ http://${os.hostname()}.local:${port}`);
|
||
console.log(` 3️⃣ http://Renauds-MacBook-Air.local:${port}\n`);
|
||
console.log('💻 Sur ce Mac, utilisez :');
|
||
console.log(` http://localhost:${port}\n`);
|
||
console.log('========================================\n');
|
||
});
|
||
|
||
server.on('error', (e) => {
|
||
if (e.code === 'EADDRINUSE') {
|
||
console.error(`❌ Le port ${port} est déjà utilisé !`);
|
||
} else {
|
||
console.error('❌ Erreur serveur:', e);
|
||
}
|
||
});
|