.env Basic Node and Express - Use the .env File file

Hello again.

You are not serving (responding) anything with the request. So, I suggest you take a look at the previous lesson, how to serve json:

Hope this helps

This part dont serve json?
app.get("/json", (req, res) => {

Not quite. It is shorthand for telling your app this:

app.route('/json').get((req, res) => {// Do something}
  • Tells your app to do something, when a GET request is made to the endpoint /json

That “something” could be “add the numbers 1 and 2”, or “respond with a json object”.

Now, the response is the thing passed to the second argument of the callback function. Information about the request is the first argument.

So, going back to the lesson:

Let’s create a simple API by creating a route that responds with JSON at the path /json . You can do it as usual, with the app.get() method. Inside the route handler, use the method res.json() , passing in an object as an argument. This method closes the request-response loop, returning the data.

I hope this clarifies.

Hello. Thank you for helping me.

I did try to do wrapping the if statement in res.json (i did also try to wrap all the if in a single res.json) but it didn’t work as well…
I still doing it wrong? (sorry im a rookie)

No worries. It is good you are learning.

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

This is correct, as far as responding to a GET request to ‘/json’. However, you should only have unique endpoints per request type.

What that means is:

  • This is fine:
app.get("/json", (req, res) => {
  res.json({message: "Hello json"});
});
app.post("/json", (req, res) => {// Do something})
  • This is not:
app.get("/json", (req, res) => {
  res.json({message: "Hello json"});
});
app.get("/json", (req, res) => {
  res.json({message: "Something else"});
});

Remember, (req, res) => { } is just like any other function.

So, if I asked you to write me a function which accepted a request and response as arguments, but responded with a different message depending on the value of MESSAGE_STYLE, how would you go about writing that?

I did try to do like the hint from the test:

app.get("/json", (req, res) => {
if (process.env.MESSAGE_STYLE === "uppercase") {
	res.json(response = "Hello World".toUpperCase());
} else {
  res.json(response = "Hello World");
}
});

Yes, and you are close, but here is the instructions:

Serve the object {"message": "Hello json"} as a response, in JSON format, to GET requests to the /json route.

… if process.env.MESSAGE_STYLE equals uppercase . The response object should become {"message": "HELLO JSON"}

Do you see the issue?

maybe this ?

app.get("/json", (req, res) => {
	var response = "Hello Json";
if (process.env.MESSAGE_STYLE === "uppercase") {
	response.toUpperCase();
	res.json({"message": ""+ response});
} else {
  res.json({"message": ""+response});
}
});

Remember, programming is usually very specific:

Serve the object {"message": "Hello json"}

var response = "Hello Json";

There is a typo in yours.

Also, this is not assigning the value to anything, so it does nothing:

response.toUpperCase();

An aside: ""+ response the "" are not necessary.

So i’ve made some changes… but its right now? if yes i think the problem is on the env file

app.get("/json", (req, res) => {
	var response = "Hello Json";
if (process.env.MESSAGE_STYLE === "uppercase") {
	response="hello json".toUpperCase();
	res.json({"message": response});
} else {
  res.json({"message": response});
}
});

There is a problem with this line:

var response = "Hello Json";

It is not exactly as it should be.

how it should be them?

maybe i can eliminate the variable doing this?

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

This is not exactly as it should be:

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

Strange, in the previous challenge they accept this model… i’ve tried both and they still fail ):

I cannot see your updated code. Could you share the link again?

There is it!

I am still seeing this:

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

The line in the else is not going pass the tests

why isn’t? this if-else is in the model shared on hint

The solution was in app.get > app.use

app.use('/json', (req, res) => {
	let response = "Hello json";

	if(process.env.MESSAGE_STYLE === 'uppercase') {
		return res.json({message:response.toUpperCase()})
	} else {
  	return res.json({message:response})
	}
})

Also, answer found here :wink:

Thx for the help :smiley:

I am glad you managed to pass.

What I was trying to get you to notice is:

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

Should be:

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

Small letter j

1 Like