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.
Solution
Sure, here are the steps to follow:
-
First, create a
.envfile in the root of your project directory. You can do this using any text editor, or from the command line using thetouchcommand like so:touch .env. -
Open the
.envfile and add the following line to it:MESSAGE_STYLE=uppercase. Save and close the file. -
Now, in your
myApp.jsfile, you need to require thedotenvpackage at the top of the file. Add this line to do so:require('dotenv').config(). -
In the
/jsonGET route handler you created in the last challenge, you need to access theMESSAGE_STYLEenvironment 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.
- Save your changes and run your server to test it. Depending on the value of
MESSAGE_STYLEin your.envfile, you should see either{"message": "Hello json"}or{"message": "HELLO JSON"}when you visithttp://localhost:3000/jsonin 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.
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.
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.