Skip to content

Instantly share code, notes, and snippets.

@FireStacks
Last active June 30, 2024 20:06
Show Gist options
  • Select an option

  • Save FireStacks/c323106fb99e18b05ad6759c79ae8335 to your computer and use it in GitHub Desktop.

Select an option

Save FireStacks/c323106fb99e18b05ad6759c79ae8335 to your computer and use it in GitHub Desktop.
Ip logger used by me

IP Logger

  • simple ip logger

Features

  • Logs the IP address and access time of each visitor
  • Stores logs in a file (ip-log.txt)
  • Simple to set up and run

Requirements

  • Node.js (v12.x or later)
  • npm (v6.x or later)

Installation

  • copy paste code
npm install express
mkdir ip-logger
cd ip-logger
npm init -y
const express = require('express');
const fs = require('fs');
const app = express();
const port = 3000;

app.use((req, res, next) => {
  const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
  const logEntry = `IP: ${ip}, Time: ${new Date().toISOString()}\n`;

  fs.appendFile('ip-log.txt', logEntry, (err) => {
    if (err) {
      console.error('Failed to log IP address:', err);
    }
  });

  next();
});

app.get('/', (req, res) => {
  res.send('Welcome to my website!');
});

app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>HTTPS</title>
</head>
<body>
  <h1>Bad Gateway</h1>
</body>
</html>

``sh node server.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment