0

Branching from the project of mine from old question.

I am trying to write NodeJS with Express to read the value of ip address on port 9999, and the internet is from the wifi hotspot sharing of my own computer, so I wrote a code like this.

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var cors = require('cors');
var dateToday = new Date();
var yearToday = dateToday.getFullYear();
var sensor_name                  = "living-room";
var hist_temperature_file_path   = "sensor-values/temperature_" + sensor_name + "_log_" + yearToday.toString() + ".csv";
var latest_temperature_file_path = "sensor-values/temperature_" + sensor_name + "_latest_value.csv";
var hist_humidity_file_path      = "sensor-values/humidity_" + sensor_name + "_log_" + yearToday.toString() + ".csv";
var latest_humidity_file_path    = "sensor-values/humidity_" + sensor_name + "_latest_value.csv";
var csv_header_temperature       = "timestamp,temperature_in_celsius\n";
var csv_header_humidity          = "timestamp,relative_humidity\n";
var sec_between_log_entries      = 60;
var latest_humidity              = 0.0;
var latest_temperature           = 0.0;
var latest_value_datetime;
var dateTime = new Date().toISOString().replace('T', ' ').substr(0, 19);
const fetch = require('node-fetch');
var ipAddress;

app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
/*app.use(function (req, res) {
  res.setHeader('Content-Type', 'application/json');
})*/
app.set('port', (process.env.PORT || 9999));
//app.set('trust proxy', true)

/*app.get('/temperature_humidity', function (req, res) {
  console.log('test1')
  latest_temperature = req.body.temperature;
  latest_humidity = req.body.humidity;
    console.log(latest_temperature)
  console.log(latest_temperature)
    res.send('success : ' + req.body.temperature + ',' + req.body.humidity)
})*/

app.get('/', function (req, res) {
  ipAddress = req.ip;
  console.log(req.ip);
})

fetch('http://' + ipAddress + '/temperature_humidity')
  .then(response => response.json())
  .then(data => {
    console.log(data);
  })

app.listen(app.get('port'), function () {
  console.log('run at port', app.get('port'));
})

But when I ran the code with the method app.get or app.post, the side of server does not show any request or response in the log, indicating no request have been sended to the server. But the terminal of NodeJS still shows run at port 9999, and no other app. log showing

The terminal log

Can you show me how to fix this problem?

P.S. The reason of fetch part was actually from how I cannot use get/post, so I use the fetch with fixed ip Address as placeholder for a while.

edit: This is the code I want to use for getting value in the first place, but it doesn't show request being sended or value being received.

app.get('/temperature_humidity', function (req, res) {
  console.log('test1')
  latest_temperature = req.body.temperature;
  latest_humidity = req.body.humidity;
    console.log(latest_temperature)
  console.log(latest_temperature)
    res.send('success : ' + req.body.temperature + ',' + req.body.humidity)
})
  • Your `fetch()` code runs before you put anything in `ipAddress`. `app.get()` just registers a handler for future requests. It doesn't execute that handler immediately. So, your `fetch()` call runs at server initialization time BEFORE any incoming requests have occurred. This code `ipAddress = req.ip;` is a bad sign. You usually don't want to be saving variables from an asynchronous operation into a higher scope because the higher scope has no idea when it's safe to use them. – jfriend00 Feb 15 '20 at 08:11
  • Do you mean for the `fetch()` call to be INSIDE the `app.get("/", ...)` handler? – jfriend00 Feb 15 '20 at 08:13
  • In the first place I want to use the `get/post` to do the reading, and fetch wasn't planned. It was a placeholder until I get a way to fix the get/post problem. – Chakrit Hinanawi Teerawijit Feb 15 '20 at 08:17
  • Well, the code you show makes no sense as shown. We can tell you some things that are wrong with it, but we can't explain what it should look like until we know what you're actually trying to accomplish. The problem here is with the `fetch()`, not with the `app.get()` though you do also need to send a response from the `app.get()`. Perhaps you don't understand that `app.get()` doesn't GET data. It sets up request handlers on your web server to RECEIVE incoming requests and then it should send a response back to the client who sent the request. – jfriend00 Feb 15 '20 at 08:20
  • I tried deleting the `fetch()` code and run only `app.get()` function, but it still doesn't send the request to the server. Not even a `console.log('test1')` text shows up. – Chakrit Hinanawi Teerawijit Feb 15 '20 at 08:24
  • Please read my previous comment about what `app.get()` actually does. I think you're confused about its purpose and thus it doesn't do what you want it to do. – jfriend00 Feb 15 '20 at 08:26
  • I see. I am very sorry for my misunderstanding. I'm sorry for asking another question. How to properly get the ip address of the server with the Express this time? – Chakrit Hinanawi Teerawijit Feb 15 '20 at 08:35
  • You can always talk to your own host with localhost or 127.0.0.1. – jfriend00 Feb 15 '20 at 08:38
  • From the link of my old project above, I am using a NodeMCU board to connect with my own computer WiFi hotspot, which makes ipaddress of it changes everytime, so I need a way to get a value of it stably even if it changes. – Chakrit Hinanawi Teerawijit Feb 15 '20 at 08:44
  • Then I'd suggest you post a new question where you state the actual problem you're trying to solve as that would allow people to better help you. You don't make a TCP connection to the hotspot. You make a TCP connection to some computer on your network. And, if you want to, you can lock it's IP address down so it is known. – jfriend00 Feb 15 '20 at 09:20

0 Answers0