Basic Node and Express - Serve JSON on a Specific Route

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

//console.log("Hello World");
app.use("/public",express.static('public'))

 app.get("/json", function(req, res) {
  res.json({"message": "Hello json"});
 //res.sendFile ( __dirname + "/views/index.html");
   
})

hello please i need your help

Please edit your post and add a link to the challenge and a description of what you need help with.

https://replit.com/@bowak/boilerplate-express-13#myApp.js
that is it

https://replit.com/@bowak/boilerplate-express-13#myApp.js

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

//console.log("Hello World");
app.use("/public",express.static('public'))


  app.get("/json", (req, res) => {
  const mySecret=process.env["MESSAGE_STYLE"]
  if(mySecret === "uppercase"){
   res.json({"message": "HELLO JSON"}); 
    
  }else{ 
    res.json({"message": "Hello json"});
  };
  
 res.sendFile ( __dirname + "/views/index.html")
  })

Are you doing this on replit? If so, could you give a link to your code on there please? (The url in the address bar for the repl).

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

When I visit your /json endpoint, I get the response { message: 'Hello json' }, when I’d expect to get { message: 'HELLO JSON' }.

Have you saved your MESSAGE_STYLE variable correctly in your Secrets tab?

Also, you appear to have altered your code from the previous steps, as the console is throwing errors.

You should be serving index.html via the root (/).

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