From dce5bceb8586ea13e7d58a8206705a8a0cc4e39f Mon Sep 17 00:00:00 2001 From: Tom <20648924+moT01@users.noreply.github.com> Date: Wed, 6 Mar 2019 17:20:17 -0600 Subject: [PATCH] fix/reformat-basic-node-express-challenges (#35424) * fix/reformat-basic-node-express-challenges * add semi-colon Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-json-on-a-specific-route.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-json-on-a-specific-route.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/get-query-parameter-input-from-the-client.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/get-route-parameter-input-from-the-client.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/implement-a-root-level-request-logger-middleware.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/implement-a-root-level-request-logger-middleware.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/implement-a-root-level-request-logger-middleware.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-an-html-file.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-static-assets.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-static-assets.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/start-a-working-express-server.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/start-a-working-express-server.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> --- ...dleware-to-create-a-time-server.english.md | 15 ++++++++++---- ...parameter-input-from-the-client.english.md | 7 +++---- ...parameter-input-from-the-client.english.md | 3 +-- ...level-request-logger-middleware.english.md | 20 +++++++++++-------- .../meet-the-node-console.english.md | 2 +- .../serve-an-html-file.english.md | 9 ++++----- .../serve-json-on-a-specific-route.english.md | 5 ++--- .../serve-static-assets.english.md | 7 +++---- .../start-a-working-express-server.english.md | 11 ++++++---- ...y-parser-to-parse-post-requests.english.md | 14 ++++++------- .../use-the-.env-file.english.md | 4 ++-- 11 files changed, 52 insertions(+), 45 deletions(-) diff --git a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/chain-middleware-to-create-a-time-server.english.md b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/chain-middleware-to-create-a-time-server.english.md index 01cb06d24e..95c0b25ea1 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/chain-middleware-to-create-a-time-server.english.md +++ b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/chain-middleware-to-create-a-time-server.english.md @@ -8,15 +8,22 @@ challengeType: 2
Middleware can be mounted at a specific route using app.METHOD(path, middlewareFunction). Middleware can also be chained inside route definition. Look at the following example: -
app.get('/user', function(req, res, next) {
req.user = getTheUserSync(); // Hypothetical synchronous operation
next();
}, function(req, res) {
res.send(req.user);
})
+
+app.get('/user', function(req, res, next) {
+  req.user = getTheUserSync(); // Hypothetical synchronous operation
+  next();
+}, function(req, res) {
+  res.send(req.user);
+}); +
This approach is useful to split the server operations into smaller units. That leads to a better app structure, and the possibility to reuse code in different places. This approach can also be used to perform some validation on the data. At each point of the middleware stack you can block the execution of the current chain and pass control to functions specifically designed to handle errors. Or you can pass control to the next matching route, to handle special cases. We will see how in the advanced Express section. -In the route app.get('/now', ...) chain a middleware function and the final handler. In the middleware function you should add the current time to the request object in the req.time key. You can use new Date().toString(). In the handler, respond with a JSON object, taking the structure {time: req.time}. -Hint: The test will not pass if you don’t chain the middleware. If you mount the function somewhere else, the test will fail, even if the output result is correct. +
## Instructions
- +In the route app.get('/now', ...) chain a middleware function and the final handler. In the middleware function you should add the current time to the request object in the req.time key. You can use new Date().toString(). In the handler, respond with a JSON object, taking the structure {time: req.time}. +Note: The test will not pass if you don’t chain the middleware. If you mount the function somewhere else, the test will fail, even if the output result is correct.
## Tests diff --git a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/get-query-parameter-input-from-the-client.english.md b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/get-query-parameter-input-from-the-client.english.md index 77fe82080c..dd7dec764b 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/get-query-parameter-input-from-the-client.english.md +++ b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/get-query-parameter-input-from-the-client.english.md @@ -6,15 +6,14 @@ challengeType: 2 ## Description
-Another common way to get input from the client is by encoding the data after the route path, using a query string. The query string is delimited by a question mark (?), and includes field=value couples. Each couple is separated by an ampersand (&). Express can parse the data from the query string, and populate the object req.query. Some characters cannot be in URLs, they have to be encoded in a different format before you can send them. If you use the API from JavaScript, you can use specific methods to encode/decode these characters. +Another common way to get input from the client is by encoding the data after the route path, using a query string. The query string is delimited by a question mark (?), and includes field=value couples. Each couple is separated by an ampersand (&). Express can parse the data from the query string, and populate the object req.query. Some characters, like the percent (%), cannot be in URLs and have to be encoded in a different format before you can send them. If you use the API from JavaScript, you can use specific methods to encode/decode these characters.
route_path: '/library'
actual_request_URL: '/library?userId=546&bookId=6754'
req.query: {userId: '546', bookId: '6754'}
-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. -TIP: In the following exercise we 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.
## Instructions
- +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.
## Tests diff --git a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/get-route-parameter-input-from-the-client.english.md b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/get-route-parameter-input-from-the-client.english.md index 8a6033c0bb..cdb957567c 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/get-route-parameter-input-from-the-client.english.md +++ b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/get-route-parameter-input-from-the-client.english.md @@ -8,12 +8,11 @@ challengeType: 2
When building an API, we have to allow users to communicate to us what they want to get from our service. For example, if the client is requesting information about a user stored in the database, they need a way to let us know which user they're interested in. One possible way to achieve this result is by using route parameters. Route parameters are named segments of the URL, delimited by slashes (/). Each segment captures the value of the part of the URL which matches its position. The captured values can be found in the req.params object.
route_path: '/user/:userId/book/:bookId'
actual_request_URL: '/user/546/book/6754'
req.params: {userId: '546', bookId: '6754'}
-Build an echo server, mounted at the route GET /:word/echo. Respond with a JSON object, taking the structure {echo: word}. You can find the word to be repeated at req.params.word. You can test your route from your browser's address bar, visiting some matching routes, e.g. your-app-rootpath/freecodecamp/echo
## Instructions
- +Build an echo server, mounted at the route GET /:word/echo. Respond with a JSON object, taking the structure {echo: word}. You can find the word to be repeated at req.params.word. You can test your route from your browser's address bar, visiting some matching routes, e.g. your-app-rootpath/freecodecamp/echo.
## Tests diff --git a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/implement-a-root-level-request-logger-middleware.english.md b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/implement-a-root-level-request-logger-middleware.english.md index e47f860f77..c4515417f6 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/implement-a-root-level-request-logger-middleware.english.md +++ b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/implement-a-root-level-request-logger-middleware.english.md @@ -6,18 +6,22 @@ challengeType: 2 ## Description
-Earlier, we introduced the express.static() middleware function. Now it’s time to see what middleware is in more detail. Middleware functions are functions that take 3 arguments: the request object, the response object, and the next function in the application’s request-response cycle. These functions execute some code that can have side effects on the app, and usually add information to the request or response objects. They can also end the cycle sending the response, when some condition is met. If they don’t send the response when they are done, they start the execution of the next function in the stack. This is triggered calling the 3rd argument next(). More information in the express documentation. -Look at the following example : -
function(req, res, next) {
console.log("I'm a middleware...");
next();
}
-Let’s suppose we mounted this function on a route. When a request matches the route, it displays the string “I’m a middleware…”. Then it executes the next function in the stack. -In this exercise we are going to build root-level middleware. As we have seen in challenge 4, to mount a middleware function at root level we can use the method app.use(<mware-function>). In this case the function will be executed for all the requests, but you can also set more specific conditions. For example, if you want a function to be executed only for POST requests, you could use app.post(<mware-function>). Analogous methods exist for all the http verbs (GET, DELETE, PUT, …). -Build a simple logger. For every request, it should log in the console a string taking the following format: method path - ip. An example would look like: GET /json - ::ffff:127.0.0.1. Note that there is a space between method and path and that the dash separating path and ip is surrounded by a space on either side. You can get the request method (http verb), the relative route path, and the caller’s ip from the request object, using req.method, req.path and req.ip. Remember to call next() when you are done, or your server will be stuck forever. Be sure to have the ‘Logs’ opened, and see what happens when some request arrives… -Hint: Express evaluates functions in the order they appear in the code. This is true for middleware too. If you want it to work for all the routes, it should be mounted before them. +Earlier, you were introduced to the express.static() middleware function. Now it’s time to see what middleware is, in more detail. Middleware functions are functions that take 3 arguments: the request object, the response object, and the next function in the application’s request-response cycle. These functions execute some code that can have side effects on the app, and usually add information to the request or response objects. They can also end the cycle by sending a response when some condition is met. If they don’t send the response when they are done, they start the execution of the next function in the stack. This triggers calling the 3rd argument, next(). +Look at the following example: +
+function(req, res, next) {
+  console.log("I'm a middleware...");
+  next();
+} +
+Let’s suppose you mounted this function on a route. When a request matches the route, it displays the string “I’m a middleware…”, then it executes the next function in the stack. +In this exercise, you are going to build root-level middleware. As you have seen in challenge 4, to mount a middleware function at root level, you can use the app.use(<mware-function>) method. In this case, the function will be executed for all the requests, but you can also set more specific conditions. For example, if you want a function to be executed only for POST requests, you could use app.post(<mware-function>). Analogous methods exist for all the HTTP verbs (GET, DELETE, PUT, …).
## Instructions
- +Build a simple logger. For every request, it should log to the console a string taking the following format: method path - ip. An example would look like this: GET /json - ::ffff:127.0.0.1. Note that there is a space between method and path and that the dash separating path and ip is surrounded by a space on both sides. You can get the request method (http verb), the relative route path, and the caller’s ip from the request object using req.method, req.path and req.ip. Remember to call next() when you are done, or your server will be stuck forever. Be sure to have the ‘Logs’ opened, and see what happens when some request arrives. +Note: Express evaluates functions in the order they appear in the code. This is true for middleware too. If you want it to work for all the routes, it should be mounted before them.
## Tests diff --git a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/meet-the-node-console.english.md b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/meet-the-node-console.english.md index 61c9745d82..a21f0319a0 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/meet-the-node-console.english.md +++ b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/meet-the-node-console.english.md @@ -7,7 +7,7 @@ challengeType: 2 ## Description
During the development process, it is important to be able to check what’s going on in your code. Node is just a JavaScript environment. Like client side JavaScript, you can use the console to display useful debug information. On your local machine, you would see the console output in a terminal. On Glitch you can open the logs in the lower part of the screen. You can toggle the log panel with the button ‘Logs’ (top-left, under the app name). -To get started, just print the classic "Hello World" in the console. We recommend to keep the log panel open while working at these challenges. Reading the logs you can be aware of the nature of the errors that may occur. +We recommend to keep the log panel open while working at these challenges. By reading the logs, you can be aware of the nature of errors that may occur.
## Instructions diff --git a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-an-html-file.english.md b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-an-html-file.english.md index dd10336f8d..899c21537d 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-an-html-file.english.md +++ b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-an-html-file.english.md @@ -6,16 +6,15 @@ challengeType: 2 ## Description
-We can respond with a file using the method res.sendFile(path). -You can put it inside the app.get('/', ...) route handler. Behind the scenes this method will set the appropriate headers to instruct your browser on how to handle the file you want to send, according to its type. Then it will read and send the file. This method needs an absolute file path. We recommend you to use the Node global variable __dirname to calculate the path. -e.g. absolutePath = __dirname + relativePath/file.ext. -The file to send is /views/index.html. Try to ‘Show Live’ your app, you should see a big HTML heading (and a form that we will use later…), with no style applied. -Note: You can edit the solution of the previous challenge, or create a new one. If you create a new solution, keep in mind that Express evaluates the routes from top to bottom. It executes the handler for the first match. You have to comment out the preceding solution, or the server will keep responding with a string. +You can respond to requests with a file using the res.sendFile(path) method. You can put it inside the app.get('/', ...) route handler. Behind the scenes, this method will set the appropriate headers to instruct your browser on how to handle the file you want to send, according to its type. Then it will read and send the file. This method needs an absolute file path. We recommend you to use the Node global variable __dirname to calculate the path like this: +
absolutePath = __dirname + relativePath/file.ext
## Instructions
+Send the /views/index.html file as a response to GET requests to the / path. If you view your live app, you should see a big HTML heading (and a form that we will use later…), with no style applied. +Note: You can edit the solution of the previous challenge or create a new one. If you create a new solution, keep in mind that Express evaluates routes from top to bottom, and executes the handler for the first match. You have to comment out the preceding solution, or the server will keep responding with a string.
## Tests diff --git a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-json-on-a-specific-route.english.md b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-json-on-a-specific-route.english.md index 2780a4134e..59d639c90e 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-json-on-a-specific-route.english.md +++ b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-json-on-a-specific-route.english.md @@ -7,13 +7,12 @@ challengeType: 2 ## Description
While an HTML server serves (you guessed it!) HTML, an API serves data. A REST (REpresentational State Transfer) API allows data exchange in a simple way, without the need for clients to know any detail about the server. The client only needs to know where the resource is (the URL), and the action it wants to perform on it (the verb). The GET verb is used when you are fetching some information, without modifying anything. These days, the preferred data format for moving information around the web is JSON. Simply put, JSON is a convenient way to represent a JavaScript object as a string, so it can be easily transmitted. -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. Behind the scenes it converts a valid JavaScript object into a string, then sets the appropriate headers to tell your browser that you are serving JSON, and sends the data back. A valid object has the usual structure {key: data}. Data can be a number, a string, a nested object or an array. Data can also be a variable or the result of a function call, in which case it will be evaluated before being converted into a string. -Serve the object {"message": "Hello json"} as a response in JSON format, to the GET requests to the route /json. Then point your browser to your-app-url/json, you should see the message on the screen. +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. Behind the scenes, it converts a valid JavaScript object into a string, then sets the appropriate headers to tell your browser that you are serving JSON, and sends the data back. A valid object has the usual structure {key: data}. data can be a number, a string, a nested object or an array. data can also be a variable or the result of a function call, in which case it will be evaluated before being converted into a string.
## Instructions
- +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.
## Tests diff --git a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-static-assets.english.md b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-static-assets.english.md index 41af72c361..e2af793870 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-static-assets.english.md +++ b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-static-assets.english.md @@ -6,14 +6,13 @@ challengeType: 2 ## Description
-An HTML server usually has one or more directories that are accessible by the user. You can place there the static assets needed by your application (stylesheets, scripts, images). In Express you can put in place this functionality using the middleware express.static(path), where the parameter is the absolute path of the folder containing the assets. If you don’t know what a middleware is, don’t worry. We’ll discuss about it later in details. Basically middlewares are functions that intercept route handlers, adding some kind of information. A middleware needs to be mounted using the method app.use(path, middlewareFunction). The first path argument is optional. If you don’t pass it, the middleware will be executed for all the requests. -Mount the express.static() middleware for all the requests with app.use(). The absolute path to the assets folder is __dirname + /public. -Now your app should be able to serve a CSS stylesheet. From outside the public folder will appear mounted to the root directory. Your front-page should look a little better now! +An HTML server usually has one or more directories that are accessible by the user. You can place there the static assets needed by your application (stylesheets, scripts, images). In Express, you can put in place this functionality using the middleware express.static(path), where the `path parameter is the absolute path of the folder containing the assets. If you don’t know what middleware is... don’t worry, we will discuss in detail later. Basically, middlewares are functions that intercept route handlers, adding some kind of information. A middleware needs to be mounted using the method app.use(path, middlewareFunction). The first path argument is optional. If you don’t pass it, the middleware will be executed for all requests.
## Instructions
- +Mount the express.static() middleware for all requests with app.use(). The absolute path to the assets folder is __dirname + /public. +Now your app should be able to serve a CSS stylesheet. From outside, the public folder will appear mounted to the root directory. Your front-page should look a little better now!
## Tests diff --git a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/start-a-working-express-server.english.md b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/start-a-working-express-server.english.md index 1c5ea7a70d..02e2aab05e 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/start-a-working-express-server.english.md +++ b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/start-a-working-express-server.english.md @@ -6,17 +6,20 @@ challengeType: 2 ## Description
-In the first two lines of the file myApp.js you can see how it’s easy to create an Express app object. This object has several methods, and we will learn many of them in these challenges. One fundamental method is app.listen(port). It tells your server to listen on a given port, putting it in running state. You can see it at the bottom of the file. It is inside comments because for testing reasons we need the app to be running in background. All the code that you may want to add goes between these two fundamental parts. Glitch stores the port number in the environment variable process.env.PORT. Its value is 3000. +In the first two lines of the file myApp.js, you can see how easy it is to create an Express app object. This object has several methods, and you will learn many of them in these challenges. One fundamental method is app.listen(port). It tells your server to listen on a given port, putting it in running state. You can see it at the bottom of the file. It is inside comments because, for testing reasons, we need the app to be running in the background. All the code that you may want to add goes between these two fundamental parts. Glitch stores the port number in the environment variable process.env.PORT. Its value is 3000. Let’s serve our first string! In Express, routes takes the following structure: app.METHOD(PATH, HANDLER). METHOD is an http method in lowercase. PATH is a relative path on the server (it can be a string, or even a regular expression). HANDLER is a function that Express calls when the route is matched. Handlers take the form function(req, res) {...}, where req is the request object, and res is the response object. For example, the handler -
function(req, res) {
res.send('Response String');
}
+
+function(req, res) {
+  res.send('Response String');
+}
will serve the string 'Response String'. -Use the app.get() method to serve the string Hello Express, to GET requests matching the / root path. Be sure that your code works by looking at the logs, then see the results in your browser, clicking the button ‘Show Live’ in the Glitch UI.
## Instructions
- +Use the app.get() method to serve the string "Hello Express" to GET requests matching the / (root) path. +Note: Be sure that your code works by looking at the logs, then see the results in your browser by clicking the ‘Show Live’ button if you are using Glitch.
## Tests diff --git a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md index 7f64a1c044..fb40871cef 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md +++ b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md @@ -6,19 +6,17 @@ challengeType: 2 ## Description
-Besides GET there is another common http verb, it is POST. POST is the default method used to send client data with HTML forms. In the REST convention POST is used to send data to create new items in the database (a new user, or a new blog post). We don’t have a database in this project, but we are going to learn how to handle POST requests anyway. -In these kind of requests the data doesn’t appear in the URL, it is hidden in the request body. This is a part of the HTML request, also called payload. Since HTML is text based, even if you don’t see the data, it doesn’t mean that they are secret. The raw content of an HTTP POST request is shown below: +Besides GET, there is another common HTTP verb, it is POST. POST is the default method used to send client data with HTML forms. In REST convention, POST is used to send data to create new items in the database (a new user, or a new blog post). You don’t have a database in this project, but you are going to learn how to handle POST requests anyway. +In these kind of requests, the data doesn’t appear in the URL, it is hidden in the request body. This is a part of the HTML request, also called payload. Since HTML is text-based, even if you don’t see the data, it doesn’t mean that it is secret. The raw content of an HTTP POST request is shown below:
POST /path/subpath HTTP/1.0
From: john@example.com
User-Agent: someBrowser/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 20
name=John+Doe&age=25
-As you can see the body is encoded like the query string. This is the default format used by HTML forms. With Ajax we can also use JSON to be able to handle data having a more complex structure. There is also another type of encoding: multipart/form-data. This one is used to upload binary files. -In this exercise we will use an urlencoded body. -To parse the data coming from POST requests, you have to install a package: the body-parser. This package allows you to use a series of middleware, which can decode data in different formats. See the docs here. -Install the body-parser module in your package.json. Then require it at the top of the file. Store it in a variable named bodyParser. -The middleware to handle url encoded data is returned by bodyParser.urlencoded({extended: false}). extended=false is a configuration option that tells the parser to use the classic encoding. When using it, values can be only strings or arrays. The extended version allows more data flexibility, but it is outmatched by JSON. Pass to app.use() the function returned by the previous method call. As usual, the middleware must be mounted before all the routes which need it. +As you can see, the body is encoded like the query string. This is the default format used by HTML forms. With Ajax, you can also use JSON to handle data having a more complex structure. There is also another type of encoding: multipart/form-data. This one is used to upload binary files. +In this exercise, you will use a urlencoded body. To parse the data coming from POST requests, you have to install the body-parser package. This package allows you to use a series of middleware, which can decode data in different formats.
## Instructions
- +Install the body-parser module in your package.json. Then, require it at the top of the file. Store it in a variable named bodyParser. The middleware to handle urlencoded data is returned by bodyParser.urlencoded({extended: false}). Pass to app.use() the function returned by the previous method call. As usual, the middleware must be mounted before all the routes which need it. +Note: extended=false is a configuration option that tells the parser to use the classic encoding. When using it, values can be only strings or arrays. The extended version allows more data flexibility, but it is outmatched by JSON.
## Tests diff --git a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-the-.env-file.english.md b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-the-.env-file.english.md index 11771254cf..a0cd22867d 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-the-.env-file.english.md +++ b/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-the-.env-file.english.md @@ -8,12 +8,12 @@ challengeType: 2
The .env file is a hidden file that is used to pass environment variables to your application. This file is secret, no one but you can access it, and it can be used to store data that you want to keep private or hidden. For example, you can store API keys from external services or your database URI. You can also use it to store configuration options. By setting configuration options, you can change the behavior of your application, without the need to rewrite some code. The environment variables are accessible from the app as process.env.VAR_NAME. The process.env object is a global Node object, and variables are passed as strings. By convention, the variable names are all uppercase, with words separated by an underscore. The .env is a shell file, so you don’t need to wrap names or values in quotes. It is also important to note that there cannot be space around the equals sign when you are assigning values to your variables, e.g. VAR_NAME=value. Usually, you will put each variable definition on a separate line. -Let's add an environment variable as a configuration option. Store the variable MESSAGE_STYLE=uppercase in the .env file. Then tell the GET /json route handler that you created in the last challenge to transform the response object’s message to uppercase if process.env.MESSAGE_STYLE equals uppercase. The response object should become {"message": "HELLO JSON"}.
## Instructions
- +Let's add an environment variable as a configuration option. +Store the variable MESSAGE_STYLE=uppercase in the .env file. Then tell the GET /json route handler that you created in the last challenge to transform the response object’s message to uppercase if process.env.MESSAGE_STYLE equals uppercase. The response object should become {"message": "HELLO JSON"}.
## Tests