Basic Node and Express - Serve JSON on a Specific Route

Tell us what’s happening:

Ive copied the exact answer. Still not getting it through. I have no clue what’s the error. Kindly help

###Your project link(s)

solution: https://3000-freecodecam-boilerplate-g8dxwnd95eg.ws-us115.gitpod.io

Your browser information:

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

Challenge Information:

Basic Node and Express - Serve JSON on a Specific Route

please post your code first.
Then also mention if you set up the .env and where did you put it.

1 Like

please copy the code and post it here instead of posting a picture of the code

You have to keep the export that is in the starting boilerplate at the bottom of the myApp.js file.

module.exports = app;

It is used in server.js for the tests.


If you need more help, share a snapshot of your Gitpod workspace or post a GitHub repo.

1 Like
const express = require('express');

const app = express();

const port = 3000; // You can choose any port number you prefer

app.get('/json', (req, res) => {

res.json({ "message": "Hello json" });

});

app.listen(port, () => {

console.log(`Server is running at http://localhost:${port}`);

});

module.exports = app;

Remove the app.listen code from myApp.js that is also handled in server.js (you should get an error about the address being in use).

Other than that, your code should pass.

const express = require(‘express’);

const app = express();

const port = 3000; // You can choose any port number you prefer

app.get(‘/json’, (req, res) => {

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

});

module.exports = app;

Still not getting through
This is the error it is showing:

Post a snapshot of your Gitpod workspace.

The not found error sounds like your server or the route isn’t serving anything. Make sure you didn’t change anything in server.js and remember to restart the server after any change you make.