0

In the following Node JS code:


import express from "express";
import pg from "pg";
import cors from "cors";
import bodyParser from "body-parser";
import { PORT, DB_USER, DB_HOST, DB_NAME, DB_PASS, DB_PORT } from "./config.js";

const { Pool } = pg;

const app = express();

app.use(express.json());
app.use(cors());
app.use(bodyParser.json());

const pool = new Pool({
  user: DB_USER,
  host: DB_HOST,
  database: DB_NAME,
  password: DB_PASS,
  port: DB_PORT,
});

app.get("/", async (req, res) => {
  const result = await pool.query(`SELECT * FROM usuarios;`);
  const rows = result.rows;
  res.json(rows);
});

app.post("/sigfox", (req, res) => {
  res.json(req.body);
  res.sendStatus(200);
});

app.listen(PORT, () => {
  console.log(`Servidor iniciado en http://localhost:`, PORT);
});

I am trying to send the temperature of my "SigFox DEVKIT" by POST method to the server, but when I enter the server I get the error "Cannot GET /sigfox".

But in the Sigfox Backend CallBack he tells me that he sent the data correctly.

[SigFox] CallBacks Results --> https://i.stack.imgur.com/tFJqv.png

I don't know why this happened. I show you my Callback Settings in case I have something wrong.

[SigFox] CallBack Settings --> https://i.stack.imgur.com/92DGL.png

Why did that happen and how can I solve it?

Previously I thought it was because I was working with a local server, but now with a hosted server I get the same error. Please, I need help.

  • Your image shows that the client is sending a POST request to the /sigfox route with the content format as application/x-www-form-urlencoded. Your nodejs code only has a route for handling GET request. – hcheung Apr 21 '23 at 01:58
  • @hcheung Thanks for answering. Currently I changed the format type to "application/json" and in the nodejs I wrote it like this ```app.post("/sigfox", (req, res) => { res.json(req.body); res.sendStatus(200); });``` but on the server the error "Cannot Get /sigfox" remains – William R. Vazquez Apr 21 '23 at 13:33

1 Answers1

0

There are two problems with your nodejs code:

  1. Your image shows that the client is sending a POST request, while your nodejs only has a route for handling Get request.
  2. The POST request from the client has a Content-Type of x-www-form-urlencoded, but your nodejs is trying to parse the data as json format with app.use(bodyParser.json());.

Here is the minimum code that should work as per your client's POST request:

import express from "express";

const PORT = 3000;
const app = express();
app.use(require('body-parser').urlencoded({ extended: false }));

app.get("/", (req, res) => {
  res.send('GET request to the server');
});

app.post("/sigfox", (req, res) => {
  res.send(res.json(req.body)); //send a response as json with req.body
});

app.listen(PORT, () => {
  console.log(`Server running at http://localhost:`, PORT);
});
hcheung
  • 3,377
  • 3
  • 11
  • 23
  • Thanks for answering. Currently I changed the format type to "application/json" and in the nodejs I wrote it like this ```app.post("/sigfox", (req, res) => { res.json(req.body); res.sendStatus(200); });``` but on the server the error "Cannot Get /sigfox" remains – William R. Vazquez Apr 21 '23 at 13:34
  • If you changed your client to send data in json, you need to change the line `app.use(require('body-parser').urlencoded({ extended: false }));` on the nodejs server to `app.use(require('body-parser').json())`. The `res.json(req.body)` only for replying back to the client, not for decoding the incoming message. – hcheung Apr 22 '23 at 09:22
  • Read the [http message](https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages) for better understanding of the http protocol. – hcheung Apr 22 '23 at 09:25
  • So how can I display the Sigfox data in JSON on that endpoint? – William R. Vazquez Apr 24 '23 at 15:00
  • We have no idea! Your question is about teh "Cannot GET" error, nothing mentioned what you want to do with the data at the endpoint of '/sigfox' other than sending the request body back with 'res.json(req.body);' in your code. – hcheung Apr 24 '23 at 22:49
  • This answer is in response to your "Cannot GET" error, if the answer help you to resolve that issue, please consider to accept the answer by clicking on the 'tick' as a courtesy. If you have other question, create a new post and explain your question. – hcheung Apr 25 '23 at 00:10