Basic Node and Express - Get Data from POST Requests

Tell us what’s happening:

get-data-from-post-requests // tests error on submit, more info below (pictures and code added)

###Your project link(s)

solution: http://localhost:3000

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36

Challenge Information:

Basic Node and Express - Get Data from POST Requests

———–

Hi,

I cannot pass this part no matter what i try. I dont really think it is about the code, though, nor the localhost. This is the code i’ve been using:

require(‘dotenv’).config();

let express = require(‘express’);

let app = express();

let bodyParser = require(‘body-parser’);

//body parser

app.use(bodyParser.urlencoded({ extended: true }));

//logger middleware

app.use(function(req, res, next) {

console.log(req.method + " " + req.path + " - " + req.ip);

next();

});

// serve css

app.use(“/public”, express.static(__dirname + “/public”));

// serve html

app.get(“/”, function(req, res) {

res.sendFile(__dirname + “/views/index.html”);

});

// use env

app.get(“/json”, function(req, res) {

let message = “Hello json”;

if (process.env.MESSAGE_STYLE === “uppercase”) {

message = message.toUpperCase();

}

res.json({ “message”: message });

});

// time server

app.get(“/now”, function(req, res, next) {

req.time = new Date().toString();

next();

}, function(req, res) {

res.json({ time: req.time });

});

//echo server

app.get(“/:word/echo”, function(req, res) {

res.json({ echo: req.params.word });

});

//get name

app.get(“/name”, function(req, res) {

var firstName = req.query.first;

var lastName = req.query.last;

res.json({ name: firstName + " " + lastName });

});

//post name

app.post(“/name”, function(req, res) {

// Handle the data in the request

var string = req.body.first + " " + req.body.last;

res.json({ name: string });

});

// server.js

module.exports = app;

and this is the response i’m having from my localhost

However, the result seems to be this:

*// running tests

  1. Test 1 : Your API endpoint should respond with the correct name

  2. Test 2 : Your API endpoint should respond with the correct name
    // tests completed*

what i found out in the browser console is the every time i hit submit only in that part of the course, this error pops up(twice, as many times as the tests run)

which is NOT the response of a falty submission by the user. Below i post a picture of one wrong answer on purpose in the GET query part

———–
So that made me think it might be a browser issue, or my OS issue worst case scenario.

However, I:

  • tunneled with ngrok, same result

  • tried with replit, same result

  • tried another browser, same result

  • tried incognito, clear cache hard reload, same result

  • i tried the ngrok like through mobile (ios), same result.

I dont really know if there’s anything else I could try.

Thanks in advance!!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.