Basic Node and Express - Serve JSON on a Specific Route

Hello,

I feel like I’ve done everything correctly on this exercise, but it is still not passing. I feel like it’s a simple fix but I’ve been stuck on it for over an hour.

Link to the challenge

My code:

let express = require('express');
let app = express();

console.log("Hello World");

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

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

//this part right here
app.get("/json", function(req, res) {
  res.json({"message": "Hello json"})
})

I have done npm install and npm start. It is listening on the proper port, but it is still not passing the tests. Could someone point me in the right direction?

Thank you.

1 Like

What is the exact error message you get? Additionally are you using Repl.it or local? I had difficulty a really long time getting Repl.it to recognize the answers for my attempt at it. Additionally local can be weird because of CORS issues.

I am doing it locally on VS Code.

And the error message is this:

The endpoint /json should serve the JSON object {"message": "Hello json"}
[Error: Not Found]

When I modify the index.html file, the changes do appear on my localhost300 page.

There is no /json path in my directory / repo, so I am wondering how a request can be made from a path that does not exist?

Allow me to answer the last question. Express is a web server. It creates a route for browsers to navigate to.

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

It creates a route for the local resource in the public directory.

But doesn’t there need to be a /json path inside the /public path, if the get request is listening for requests that originate from the /json path?

My code looks exactly like the FCC hint / answer, so idk what is wrong. Maybe there is some boilerplate code that is missing or I’m leaving out something that was never explained in the instructions.

Nope. Completely unnecessary.

app.get("/json", function(req, res) {
  res.json({"message": "Hello json"})
})

This code says that if a client sends a get request to the /json path on this web server, send a JSON object consisting of a message string set to “Hello json”.

1 Like

It’s more like the test for this part of the curriculum is buggy. Try again using Repl.it and tell me what response you get then.

1 Like

@camperextraordinaire

The .env lesson is actually the next exercise after this, but it doesn’t mention anything about using or needing .env to run things locally. It just teaches how to store and access global variables from an .env file, and then has you format the response message to uppercase depending on the value of the MESSAGE_STYLE variable.

But here is what my something.env file looks like.

MESSAGE_STYLE=uppercase

And here is my myApp.js file after the .env lesson.

let express = require('express');
let app = express();
require('dotenv').config()

console.log("Hello World");

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

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

app.get("/json", function(req, res) {
  if (process.env.MESSAGE_STYLE===uppercase) {
    res.message = res.message.toUpperCase();
  }
  res.json({"message": "Hello json"});
})

Pardon me, but the .env file is not actually mentioned in this exercise. It is surprising to see this individual get an “[Error:Not Found]” though. The only browser it works properly is in Google Chrome. The other browsers seem to be more properly locked down, which I don’t know if it is a good or bad thing. Additionally the error I get in other browsers is this

@a2937

I tried it on replit but I am still getting the same error. I’m not sure if I entered the URL properly. I used the actual replit page URL, and then I published the replit and tried copying the URL they gave me. Neither of them are passing the tests. Unfortunately.

@camperextraordinaire @a2937

Thank you both for your help.

Here is the Replit link

Okay. I forked the Replit but I’m not sure what the live URL is.

I entered that, but FCC is telling me to “please enter a URL.”

@camperextraordinaire

Praise the sun it works! Thank you very much, Randall. You are the GOAT.

Hi, I’m using Replit, and I’m still getting errors. I’m not sure what exactly the problem is but whenever I type /json in the web view, it returns not found

let express = require('express');
let app = express();

app.get("/json", function(req, res) {
  res.json({"message": "Hello json"})
})

Have you try to add the cors? It works for me. Remember to restart the app after any change.

import express from 'express';
import cors from 'cors';

const app = express();

app.use(cors());

app.get('/json', (req, res) => {
  return res.status(200).json({"message": "Hello json"});
});

Please don’t hijack someone else’s thread.

I did now and it still didn’t work

Sorry for that, you help the other person and what I did was related so I thought I could use this, but I will create another thread. Thanks