Basic Node and Express - Get Data from POST Requests

Tell us what’s happening:

Server returns the correct json object after submitting the form but both tests fail with the message: “Your API endpoint should respond with the correct name” and i dont know what to do.

###Your project link(s)

solution:

let express = require('express');
const { log } = require('fcc-express-bground');
require('dotenv').config();
let bodyParser = require('body-parser');
let app = express();

console.log("Hello World");

app.use((req, res, next) => {
    console.log(req.method + ' ' + req.path + ' - ' + req.ip);
    next();
});

app.use("/public", express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({ extended: false }));

app.get("/", (req, res) => {
    res.sendFile(__dirname + "/views/index.html");
});

app.get("/:word/echo", (req, res) => {
    res.json({ "echo": req.params.word });
});

app.get("/name", (req, res) => {
    res.json({ "name": req.query.first + " " + req.query.last });
});

app.post("/name", (req, res) => {
    res.json({ "name": req.body.first + " " + req.body.last });
});

app.get("/now", (req, res, next) => {
    req.time = new Date().toString();
    next();
}, (req, res) => {
    res.json({ "time": req.time });
});

app.get("/json", (req, res) => {
    if (process.env.MESSAGE_STYLE == "uppercase") {
        res.json({ "message": "HELLO JSON" });
    } else {
        res.json({ "message": "Hello json" });
    }
});



module.exports = app;

Your browser information:

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

Challenge Information:

Basic Node and Express - Get Data from POST Requests

1 Like

I see your endpoint is returning JSON, which is good. The tests usually fail if something is slightly off in formatting.

Try verifying:

  • The JSON key is exactly "name".
  • There’s exactly one space between the first and last names.
  • For GET requests, you’re using req.query; for POST, use req.body.

Even small mismatches here can make the tests fail.

if you open the browser console when you run the tests you should see the full output of the tests there, maybe that can give you more infos on what’s wrong

i get two instances of the error: DataCloneError. URLSearchParams object could not be cloned.
Screenshot:

1 Like

are those things you use in your app? if not this may be something to report as a bug

The code I provided in my original post is all I use. I will report it thank you

Hi, I have the same problem. Sorry I have no answer to that but it seems to be a bug in the tests. I thought it wouldn’t make sense making another post so yeah.