Basic Node and Express - Serve JSON on a Specific Route

Tell us what’s happening:
Describe your issue in detail here.

endpoint /json should serve the JSON object {"message": "Hello json"}

i compared with

the tip is this:

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

i dont see the error in my source of the app:

let express = require('express');
let app = express();
const path = require('path');
app.listen(80);
console.log("Hello Express");
// console.log('__dirname=' + __dirname); // __dirname=/home/runner/boilerplate-express
app.get("/", (req, res) => {
  // res.sendFile('/views/index.html');
  res.sendFile(path.join(__dirname, '/views/index.html'));
});
app.get("/json", (req, res) => {
  res.json({
    message: "Hello json"
  });
});
// Assets at the /public route
app.use('/public', express.static(__dirname + '/public'));
module.exports = app;

Your project link(s)

solution: boilerplate-express - Replit

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0

Challenge: Basic Node and Express - Serve JSON on a Specific Route

Link to the challenge:

Your repl passes the challenge for me.

i deleted a
console.log("Hello Express");
somewhere above. that solves it.

Why do you have this line of code in myApp.js?

app.listen(80);

it solved a problem in a task before. or it feels for me so. at the end the task was solved.
so you mean i dont need it? ok, then better i delete it.

The server code is all in the server.js file. You don’t need this here.

1 Like

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