fix(curriculum): Convert blockquote elements to triple backtick syntax for Apis And Microservices (#35996)

* fix: converted blockquotes

* fix: revert to blockquote

* fix: changed js to http

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: reverted back to blockquote

* fix: reverted back to blockquote

* fix: reverted back to blockquote
This commit is contained in:
Randell Dawson
2019-05-14 05:00:06 -07:00
committed by Tom
parent 46411ca1cd
commit 4dbd496b49
16 changed files with 106 additions and 45 deletions

View File

@ -8,14 +8,16 @@ challengeType: 2
<section id='description'>
Middleware can be mounted at a specific route using <code>app.METHOD(path, middlewareFunction)</code>. Middleware can also be chained inside route definition.
Look at the following example:
<blockquote>
app.get('/user', function(req, res, next) {<br>
&nbsp;&nbsp;req.user = getTheUserSync(); // Hypothetical synchronous operation<br>
&nbsp;&nbsp;next();<br>
}, function(req, res) {<br>
&nbsp;&nbsp;res.send(req.user);<br>
```js
app.get('/user', function(req, res, next) {
req.user = getTheUserSync(); // Hypothetical synchronous operation
next();
}, function(req, res) {
res.send(req.user);
});
</blockquote>
```
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.
</section>

View File

@ -8,12 +8,14 @@ challengeType: 2
<section id='description'>
Earlier, you were introduced to the <code>express.static()</code> middleware function. Now its 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 applications 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 dont send the response when they are done, they start the execution of the next function in the stack. This triggers calling the 3rd argument, <code>next()</code>.
Look at the following example:
<blockquote>
function(req, res, next) {<br>
&nbsp;&nbsp;console.log("I'm a middleware...");<br>
&nbsp;&nbsp;next();<br>
```js
function(req, res, next) {
console.log("I'm a middleware...");
next();
}
</blockquote>
```
Lets suppose you mounted this function on a route. When a request matches the route, it displays the string “Im 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 <code>app.use(&lt;mware-function&gt;)</code> 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 <code>app.post(&lt;mware-function&gt;)</code>. Analogous methods exist for all the HTTP verbs (GET, DELETE, PUT, …).
</section>

View File

@ -7,7 +7,11 @@ challengeType: 2
## Description
<section id='description'>
You can respond to requests with a file using the <code>res.sendFile(path)</code> method. You can put it inside the <code>app.get('/', ...)</code> 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 <code>__dirname</code> to calculate the path like this:
<blockquote>absolutePath = __dirname + relativePath/file.ext</blockquote>
```js
absolutePath = __dirname + relativePath/file.ext
```
</section>
## Instructions

View File

@ -9,10 +9,13 @@ challengeType: 2
In the first two lines of the file <code>myApp.js</code>, 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 <code>app.listen(port)</code>. 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 <code>process.env.PORT</code>. Its value is <code>3000</code>.
Lets serve our first string! In Express, routes takes the following structure: <code>app.METHOD(PATH, HANDLER)</code>. 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 <code>function(req, res) {...}</code>, where req is the request object, and res is the response object. For example, the handler
<blockquote>
function(req, res) {<br>
&nbsp;&nbsp;res.send('Response String');<br>
}</blockquote>
```js
function(req, res) {
res.send('Response String');
}
```
will serve the string 'Response String'.
</section>

View File

@ -8,7 +8,16 @@ challengeType: 2
<section id='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 REST convention, POST is used to send data to create new items in the database (a new user, or a new blog post). You dont 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 doesnt 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 dont see the data, it doesnt mean that it is secret. The raw content of an HTTP POST request is shown below:
<blockquote>POST /path/subpath HTTP/1.0<br>From: john@example.com<br>User-Agent: someBrowser/1.0<br>Content-Type: application/x-www-form-urlencoded<br>Content-Length: 20<br>name=John+Doe&age=25</blockquote>
```http
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, 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 <code>body-parser</code> package. This package allows you to use a series of middleware, which can decode data in different formats.
</section>