Knowee
Questions
Features
Study Tools

Create a .env file in the root of your project directory, and store the variable MESSAGE_STYLE=uppercase in it.Then, in the /json GET route handler you created in the last challenge access process.env.MESSAGE_STYLE and transform the response object's message to uppercase if the variable equals uppercase. The response object should either be {"message": "Hello json"} or {"message": "HELLO JSON"}, depending on the MESSAGE_STYLE value. Note that you must read the value of process.env.MESSAGE_STYLE inside the route handler, not outside of it, due to the way our tests run.If you are working locally, you will need the dotenv package. It loads environment variables from your .env file into process.env. The dotenv package has already been installed, and is in your project's package.json file. At the top of your myApp.js file, add require('dotenv').config() to load the environment variables.

Question

Create a .env file in the root of your project directory, and store the variable MESSAGE_STYLE=uppercase in it.Then, in the /json GET route handler you created in the last challenge access process.env.MESSAGE_STYLE and transform the response object's message to uppercase if the variable equals uppercase. The response object should either be {"message": "Hello json"} or {"message": "HELLO JSON"}, depending on the MESSAGE_STYLE value. Note that you must read the value of process.env.MESSAGE_STYLE inside the route handler, not outside of it, due to the way our tests run.If you are working locally, you will need the dotenv package. It loads environment variables from your .env file into process.env. The dotenv package has already been installed, and is in your project's package.json file. At the top of your myApp.js file, add require('dotenv').config() to load the environment variables.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here are the steps to follow:

  1. First, create a .env file in the root of your project directory. You can do this using any text editor, or from the command line using the touch command like so: touch .env.

  2. Open the .env file and add the following line to it: MESSAGE_STYLE=uppercase. Save and close the file.

  3. Now, in your myApp.js file, you need to require the dotenv package at the top of the file. Add this line to do so: require('dotenv').config().

  4. In the /json GET route handler you created in the last challenge, you need to access the MESSAGE_STYLE environment variable and use it to determine the case of the response message. Here's how you can do it:

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

In this code, we first set message to "Hello json". Then, we check if process.env.MESSAGE_STYLE equals "uppercase". If it does, we transform message to uppercase. Finally, we send a JSON response with message.

  1. Save your changes and run your server to test it. Depending on the value of MESSAGE_STYLE in your .env file, you should see either {"message": "Hello json"} or {"message": "HELLO JSON"} when you visit http://localhost:3000/json in your web browser.

Remember, the dotenv package allows us to load environment variables from our .env file into process.env, so we can use them in our application.

This problem has been solved

Similar Questions

Serve the object {"message": "Hello json"} as a response, in JSON format, to GET requests to the /json route. Then point your browser to your-app-url/json, you should see the message on the screen.

Build an API endpoint, mounted at GET /name. Respond with a JSON document, taking the structure { name: 'firstname lastname'}. The first and last name parameters should be encoded in a query string e.g. ?first=firstname&last=lastname.Note: In the following exercise you are going to receive data from a POST request, at the same /name route path. If you want, you can use the method app.route(path).get(handler).post(handler). This syntax allows you to chain different verb handlers on the same path route. You can save a bit of typing, and have cleaner code.

What does Flask return when a route handler function returns a string?1 pointA JSON objectAn HTTP response with the string in the bodyAn errorA template render

What request methods does this line of code below [email protected]('/test')

In the following exercise you are going to receive data from a POST request, at the same /name route path. If you want, you can use the method app.route(path).get(handler).post(handler). This syntax allows you to chain different verb handlers on the same path route. You can save a bit of typing, and have cleaner code.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.