0

Excuse me for my English but it's my first post. I think I have a little problem.

When a client interrogates my API server, for example, to search for content. URL GET METHOD: http://localhost:9000/5e70da6c2af6194004aa9694

The URL returns this : le document avec l'ID 5e70da6c2af6194004aa9694 est trouvé

In backend my code it's :

app.get('/:id',  (req, res) => {

    mongoose.connect(connect.uriMd , connect.optionsMd ).then(// Start Connection
        (err, db) => {/** ready to use. The `mongoose.connect()` promise resolves to mongoose instance. */
            logMsg.logMsg('info', `${connect.succes}`);

            const id = req.params.id;
            mb_js.findOne({_id : id}, (err, htmlElm) => {
                if (err) res.json( err );

                if (htmlElm) {
                    logMsg.logMsg('info', `le document avec l'ID ${id} est trouvé`); 
                    //console.log (`le document avec l'ID ${id} est trouvé`);
                    res.json(htmlElm.dataHtml);
                } else {
                    logMsg.logMsg('info', `le document avec l'ID ${id} est introuvable`); 
                    //console.log (`le document avec l'ID ${id} est introuvable`);
                    res.json(`l'ID : ${id} n'existe pas`);
                }

                mongoose.connection.close((err, res) => {
                    if (err)  throw err; 
                    logMsg.logMsg('info', `${connect.close}`);     
                });

            });

        },// END GOOF CONNECTION

        err => {
            logMsg.logMsg(`fatal`, `${connect.error}`);
            res.send(`${connect.error}`);
            return;
        }      
    );
})

So when my API server analyses this route : app.get('/:id', (req, res) => {

with req object is it possible to get the client IP address? "it's an HTTP server API"

thank you for your answers

Piotr Niewinski
  • 1,158
  • 1
  • 12
  • 25
Tinquiet
  • 1
  • 3
  • Does this answer your question? [Express.js: how to get remote client address](https://stackoverflow.com/questions/10849687/express-js-how-to-get-remote-client-address) – Stephen S Mar 17 '20 at 16:20
  • thanks but no. when i use this console.log(req.ip); console.log(req.ips); console.log(req.connection.remoteAddress); the respons is : ::1 [] ::1 SO is empty – Tinquiet Mar 17 '20 at 17:16
  • 1
    That's because you use localhost which is a loopback address. If you use the server with an IP address, you should get the client's address – Stephen S Mar 17 '20 at 17:23
  • Ok ! this is my problem ! thanks you Stephen – Tinquiet Mar 17 '20 at 17:32

0 Answers0