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
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)
})