title
Node.js Crash Course Tutorial #4 - Requests & Responses

description
Hey gang, in this Node.js tutorial we'll take a look at how we can gather information from a user request & then send back a response to the browser. 0:00 -- request object 3:22 -- response object 9:05 -- returning html pages 12:59 -- basic routing 18:41 -- status codes 22:17 -- redirects 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: https://github.com/iamshaunjp/node-crash-course 🐱‍💻 🐱‍💻 Other Related Free Courses: + Modern JavaScript Tutorial - https://www.youtube.com/playlist?list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc + HTML & CSS Crash Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G 🐱‍💻 🐱‍💻 Node.js links + Download here - https://nodejs.org/en/download/

detail
{'title': 'Node.js Crash Course Tutorial #4 - Requests & Responses', 'heatmap': [{'end': 358.09, 'start': 319.484, 'weight': 0.786}, {'end': 1101.785, 'start': 1065.188, 'weight': 0.739}, {'end': 1527.785, 'start': 1484.346, 'weight': 0.818}], 'summary': 'Tutorial covers setting up a node server to handle requests, logging request details, sending html responses and files, handling server responses, and implementing redirects in node.js, emphasizing the significance of restart after code changes and the transition to using express for efficient route and request management.', 'chapters': [{'end': 89.808, 'segs': [{'end': 70.501, 'src': 'embed', 'start': 2.529, 'weight': 0, 'content': [{'end': 3.27, 'text': 'all right, then gang.', 'start': 2.529, 'duration': 0.741}, {'end': 10.116, 'text': "so now we've created our server right here and we're saying we want the server to listen to port 3000 on localhost.", 'start': 3.27, 'duration': 6.846}, {'end': 18.044, 'text': 'so when we send requests to this localhost, port 3000, then we should fire this function right here now.', 'start': 10.116, 'duration': 7.928}, {'end': 20.706, 'text': 'first of all, we need to actually run our file for this to work.', 'start': 18.044, 'duration': 2.662}, {'end': 22.088, 'text': 'so let me do that again.', 'start': 20.706, 'duration': 1.382}, {'end': 26.212, 'text': "node server and now we're listening for requests on port 3000.", 'start': 22.088, 'duration': 4.124}, {'end': 33.36, 'text': 'so if i go over here and refresh this, then we should see down here request made, because this function logs that to the console.', 'start': 26.212, 'duration': 7.148}, {'end': 36.402, 'text': 'now, what if we make a change to this?', 'start': 34.281, 'duration': 2.121}, {'end': 45.107, 'text': 'what if instead we want to log out, for example, the request object and the request object contains information about the request that a user sends.', 'start': 36.402, 'duration': 8.705}, {'end': 45.988, 'text': 'so let me do that.', 'start': 45.107, 'duration': 0.881}, {'end': 50.911, 'text': "let me delete this and instead log out the request object, and i've saved that.", 'start': 45.988, 'duration': 4.923}, {'end': 54.353, 'text': "and now i'm going to go to the browser, stop this and refresh.", 'start': 50.911, 'duration': 3.442}, {'end': 58.135, 'text': "come over here and we don't see the request object log to the console.", 'start': 54.353, 'duration': 3.782}, {'end': 59.656, 'text': 'we just see request made.', 'start': 58.135, 'duration': 1.521}, {'end': 66.699, 'text': "That's because every time we make a change we have to restart our file.", 'start': 60.856, 'duration': 5.843}, {'end': 69.34, 'text': "It's still running the old file and it's not picked up.", 'start': 66.699, 'duration': 2.641}, {'end': 70.501, 'text': 'the code change.', 'start': 69.34, 'duration': 1.161}], 'summary': 'Created server to listen on port 3000, logged request info, but need to restart for changes to take effect.', 'duration': 67.972, 'max_score': 2.529, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk2529.jpg'}], 'start': 2.529, 'title': 'Node server and request handling', 'summary': 'Demonstrates setting up a node server to listen to port 3000, handling requests, and emphasizes the need to restart the server after code changes.', 'chapters': [{'end': 89.808, 'start': 2.529, 'title': 'Node server and request handling', 'summary': 'Demonstrates setting up a node server to listen to port 3000, handling requests and the need to restart the server after code changes.', 'duration': 87.279, 'highlights': ['The server is set to listen to port 3000 on localhost for incoming requests.', 'Making changes to the code requires restarting the server to ensure the latest code changes are picked up.', 'Logging out the request object provides information about the request sent by the user.']}], 'duration': 87.279, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk2529.jpg', 'highlights': ['The server is set to listen to port 3000 on localhost for incoming requests.', 'Making changes to the code requires restarting the server to ensure the latest code changes are picked up.', 'Logging out the request object provides information about the request sent by the user.']}, {'end': 364.495, 'segs': [{'end': 136.88, 'src': 'embed', 'start': 89.808, 'weight': 0, 'content': [{'end': 100.891, 'text': "so if I press enter over here and we should see the request object now, it's a huge object with loads of different things on the request object.", 'start': 89.808, 'duration': 11.083}, {'end': 108.333, 'text': 'it contains headers, which is metadata about the request, the type of request, the expected response types, etc.', 'start': 100.891, 'duration': 7.442}, {'end': 112.794, 'text': 'it also contains the url that a user has visited up here.', 'start': 108.333, 'duration': 4.461}, {'end': 115.215, 'text': 'it contains loads of other stuff as well.', 'start': 112.794, 'duration': 2.421}, {'end': 118.635, 'text': 'for now let me just log out a few different things from this.', 'start': 115.215, 'duration': 3.42}, {'end': 125.237, 'text': "so i'm going to say, for example, the request.url and also the request.method.", 'start': 118.635, 'duration': 6.602}, {'end': 127.117, 'text': 'so that is the type of request.', 'start': 125.237, 'duration': 1.88}, {'end': 129.138, 'text': 'is it a get request, a post request, etc.', 'start': 127.117, 'duration': 2.021}, {'end': 131.658, 'text': 'so let me save this.', 'start': 129.997, 'duration': 1.661}, {'end': 133.739, 'text': 'we have to restart the server.', 'start': 131.658, 'duration': 2.081}, {'end': 136.88, 'text': "so I've cancelled out of the process by clicking ctrl C.", 'start': 133.739, 'duration': 3.141}], 'summary': "The request object contains headers, url, and method; it's a huge object with various details.", 'duration': 47.072, 'max_score': 89.808, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk89808.jpg'}, {'end': 213.396, 'src': 'embed', 'start': 180.047, 'weight': 2, 'content': [{'end': 185.031, 'text': 'and If they go to just forward slash, we want to send back an index page or a home page.', 'start': 180.047, 'duration': 4.984}, {'end': 189.014, 'text': 'If they go to forward slash about, we might want to send back an about page.', 'start': 185.031, 'duration': 3.983}, {'end': 196.901, 'text': 'So we can use that information, this thing, right here, later on when we come to implement some kind of simple routing and we will see that.', 'start': 189.014, 'duration': 7.887}, {'end': 200.924, 'text': "But first of all, let's also take a look at this thing right here, the response object.", 'start': 196.901, 'duration': 4.023}, {'end': 213.396, 'text': 'so then we also have access to this response object right here, which is what we use to send a response to the browser.', 'start': 205.808, 'duration': 7.588}], 'summary': 'Implement simple routing using the response object.', 'duration': 33.349, 'max_score': 180.047, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk180047.jpg'}, {'end': 319.484, 'src': 'embed', 'start': 253.152, 'weight': 3, 'content': [{'end': 260.956, 'text': 'and the way we set a header is by using the response object and then using the method set Header like so.', 'start': 253.152, 'duration': 7.804}, {'end': 263.817, 'text': 'now we want to set the content type header.', 'start': 260.956, 'duration': 2.861}, {'end': 268.12, 'text': "There's lots of different types of headers and I'm going to show you a couple in a second.", 'start': 263.817, 'duration': 4.303}, {'end': 276.144, 'text': "But I'm going to say the content type, and this could be either Text, or it could be HTML, or it could be JSON.", 'start': 268.12, 'duration': 8.024}, {'end': 282.227, 'text': "Like I said, we're going to say for now We're going to use text Forward slash plane,", 'start': 276.164, 'duration': 6.063}, {'end': 286.55, 'text': "and that just means we're sending back some plain text to the browser.", 'start': 282.227, 'duration': 4.323}, {'end': 290.812, 'text': 'So how do we actually send that data back to the browser?', 'start': 286.55, 'duration': 4.262}, {'end': 297.236, 'text': 'Well, again we use the response object and we use the right method to write to the response.', 'start': 291.373, 'duration': 5.863}, {'end': 302.48, 'text': "and let's say, for example, I just want to say hello, ninjas, and send that text back to the browser.", 'start': 297.236, 'duration': 5.244}, {'end': 304.9, 'text': "and well, i'm writing to the response.", 'start': 302.48, 'duration': 2.42}, {'end': 310.622, 'text': "but now we have to end the response to say okay, we've ended this, now send it back to the browser.", 'start': 304.9, 'duration': 5.722}, {'end': 313.923, 'text': 'so we do that by saying response dot end.', 'start': 310.622, 'duration': 3.301}, {'end': 315.283, 'text': 'so three steps.', 'start': 313.923, 'duration': 1.36}, {'end': 319.484, 'text': "first of all, we're setting the header for the content type being sent back to the browser.", 'start': 315.283, 'duration': 4.201}], 'summary': 'Setting content type header as text/plain and sending data to browser using response object in 3 steps.', 'duration': 66.332, 'max_score': 253.152, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk253152.jpg'}, {'end': 367.217, 'src': 'heatmap', 'start': 319.484, 'weight': 5, 'content': [{'end': 323.265, 'text': "then we're writing whatever content we want to send back to the browser.", 'start': 319.484, 'duration': 3.781}, {'end': 326.966, 'text': "then we're ending the response, which then sends it to the browser.", 'start': 323.265, 'duration': 3.701}, {'end': 334.352, 'text': 'So if I save this, I need to come down here and cancel out of this process and run node server again.', 'start': 327.606, 'duration': 6.746}, {'end': 339.716, 'text': "Then I'm gonna just refresh over here and we should see, hello ninjas, this text right here.", 'start': 334.352, 'duration': 5.364}, {'end': 341.437, 'text': 'Let me zoom in a little bit.', 'start': 339.756, 'duration': 1.681}, {'end': 348.263, 'text': 'Okay, so let me just inspect the element right here and then go to the network tab.', 'start': 341.437, 'duration': 6.826}, {'end': 351.745, 'text': "I'm going to refresh so we can see this request down here.", 'start': 348.263, 'duration': 3.482}, {'end': 354.267, 'text': "this is the request we've made up here.", 'start': 351.745, 'duration': 2.522}, {'end': 358.09, 'text': 'now, if I click on this, we can see the headers right here.', 'start': 354.267, 'duration': 3.823}, {'end': 364.495, 'text': "the response headers are right here and you can see this is the one that we've set, the content type,", 'start': 358.09, 'duration': 6.405}, {'end': 367.217, 'text': 'but we also have these ones automatically set as well.', 'start': 364.495, 'duration': 2.722}], 'summary': 'Setting response content and headers, testing with browser refresh.', 'duration': 47.733, 'max_score': 319.484, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk319484.jpg'}], 'start': 89.808, 'title': 'Request logging and header setting in node.js', 'summary': 'Covers logging request details such as urls and method types and its relevance for implementing routing. it also discusses setting response headers in node.js to send specific content types and provides an example of sending text to the browser while inspecting response headers.', 'chapters': [{'end': 200.924, 'start': 89.808, 'title': 'Logging request details and simple routing', 'summary': 'Covers logging request details including url and method types, and highlights the relevance of this information for implementing simple routing in the future.', 'duration': 111.116, 'highlights': ['The request object contains headers, url, and method type, providing metadata about the request and the type of request (e.g., get or post).', 'Logging the request.url and request.method helps in understanding the URL and method type, which is crucial for implementing simple routing.', "The URL information is essential for sending different responses based on the route, such as sending back an index page for the root or an about page for '/about'."]}, {'end': 364.495, 'start': 205.808, 'title': 'Setting response headers in node.js', 'summary': "Discusses setting response headers in node.js to send a specific content type (text, html, json) to the browser, writing the content to be sent back, and ending the response, with an example of sending 'hello ninjas' as plain text to the browser, and demonstrates inspecting the response headers using the network tab.", 'duration': 158.687, 'highlights': ['The chapter discusses setting response headers in Node.js to send a specific content type (text, html, json) to the browser', 'Writing the content to be sent back', "Ending the response, with an example of sending 'hello ninjas' as plain text to the browser", 'Demonstrates inspecting the response headers using the network tab']}], 'duration': 274.687, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk89808.jpg', 'highlights': ['The request object contains headers, url, and method type, providing metadata about the request and the type of request (e.g., get or post).', 'Logging the request.url and request.method helps in understanding the URL and method type, which is crucial for implementing simple routing.', "The URL information is essential for sending different responses based on the route, such as sending back an index page for the root or an about page for '/about'.", 'The chapter discusses setting response headers in Node.js to send a specific content type (text, html, json) to the browser', "Ending the response, with an example of sending 'hello ninjas' as plain text to the browser", 'Demonstrates inspecting the response headers using the network tab', 'Writing the content to be sent back']}, {'end': 679.274, 'segs': [{'end': 419.019, 'src': 'embed', 'start': 390.547, 'weight': 3, 'content': [{'end': 392.788, 'text': 'we can send back an html string.', 'start': 390.547, 'duration': 2.241}, {'end': 396.75, 'text': 'so i could say p tag right here and close the p tag right there.', 'start': 392.788, 'duration': 3.962}, {'end': 400.711, 'text': 'also, if I wanted to add another line of code, I could do.', 'start': 397.39, 'duration': 3.321}, {'end': 405.573, 'text': 'let me just duplicate this and say hello again, ninjas.', 'start': 400.711, 'duration': 4.862}, {'end': 414.177, 'text': 'so first we write the first line of HTML to the response, then we add another line of HTML and it writes both of those one after the other.', 'start': 405.573, 'duration': 8.604}, {'end': 419.019, 'text': 'then we end the response which sends all of the resulting code back to the browser.', 'start': 414.177, 'duration': 4.842}], 'summary': 'Demonstrating sending html string with tags and content.', 'duration': 28.472, 'max_score': 390.547, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk390547.jpg'}, {'end': 463.047, 'src': 'embed', 'start': 433.896, 'weight': 2, 'content': [{'end': 438.677, 'text': 'now notice the browser has automatically added the HTML, the head and the body tags.', 'start': 433.896, 'duration': 4.781}, {'end': 443.298, 'text': 'but if we were to write our own right here, then it would replace those.', 'start': 438.677, 'duration': 4.621}, {'end': 444.578, 'text': 'and let me just demo that.', 'start': 443.298, 'duration': 1.28}, {'end': 452.621, 'text': "if I just say up here response dot right and then inside here I'm just going to do, for example,", 'start': 444.578, 'duration': 8.043}, {'end': 463.047, 'text': "a head tag and a closing head and inside that we'll just do a link tag like so,", 'start': 452.621, 'duration': 10.426}], 'summary': 'Demonstrating how to replace automatically added html tags with custom ones in the browser.', 'duration': 29.151, 'max_score': 433.896, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk433896.jpg'}, {'end': 515.684, 'src': 'embed', 'start': 487.518, 'weight': 4, 'content': [{'end': 492.241, 'text': "so we don't have to keep cancelling out the process and running it again later on in the course.", 'start': 487.518, 'duration': 4.723}, {'end': 498.014, 'text': 'if i refresh now, by the way, we can open the head and we can see that that link tag is right there.', 'start': 493.331, 'duration': 4.683}, {'end': 504.238, 'text': 'so if we create our own and write that to the response, it replaces the default one for us.', 'start': 498.014, 'duration': 6.224}, {'end': 508.661, 'text': 'okay, so this is how easy it is to actually send a response.', 'start': 504.238, 'duration': 4.423}, {'end': 511.543, 'text': 'but you can already see that this is looking quite messy.', 'start': 508.661, 'duration': 2.882}, {'end': 515.684, 'text': 'if we want to send some html back and this is just some very, very,', 'start': 511.543, 'duration': 4.141}], 'summary': 'Demonstrating how to easily send a response with html, replacing default link tag.', 'duration': 28.166, 'max_score': 487.518, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk487518.jpg'}, {'end': 580.765, 'src': 'embed', 'start': 531.313, 'weight': 0, 'content': [{'end': 540.977, 'text': 'ideally, our HTML pages should be created in a separate HTML file, and then node could maybe read those files and send them back to the browser,', 'start': 531.313, 'duration': 9.664}, {'end': 544.439, 'text': "and to do this we're going to need to make use of the file system again.", 'start': 540.977, 'duration': 3.462}, {'end': 550.207, 'text': 'All right, then.', 'start': 549.727, 'duration': 0.48}, {'end': 559.054, 'text': "So we've seen how to send HTML this way, but let's see how we can actually send a full HTML page, a full document, to the browser instead,", 'start': 550.307, 'duration': 8.747}, {'end': 560.696, 'text': 'which is a little bit cleaner.', 'start': 559.054, 'duration': 1.642}, {'end': 566.14, 'text': "So the first thing I'm going to do is create a new folder over here, and I'm going to call that views.", 'start': 561.216, 'duration': 4.924}, {'end': 568.522, 'text': "And this is where I'm going to store all my HTML.", 'start': 566.28, 'duration': 2.242}, {'end': 576.424, 'text': "so inside there i'm going to create an index.html file and this is what i want to send back to the browser.", 'start': 569.122, 'duration': 7.302}, {'end': 580.765, 'text': "now i'm just going to paste this code in so you don't have to watch me type it all out.", 'start': 576.424, 'duration': 4.341}], 'summary': "Creating a separate html file and using node to send it back to the browser, storing html in a 'views' folder.", 'duration': 49.452, 'max_score': 531.313, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk531313.jpg'}, {'end': 637.956, 'src': 'embed', 'start': 602.993, 'weight': 1, 'content': [{'end': 608.676, 'text': 'we still want to set the header to be of content type, text forward, slash, HTML.', 'start': 602.993, 'duration': 5.683}, {'end': 617.582, 'text': 'but this time we want to send back a file, or rather read a file and then send the data from that file as a response to the browser.', 'start': 608.676, 'duration': 8.906}, {'end': 619.783, 'text': "now we've already seen how we can read files.", 'start': 617.582, 'duration': 2.201}, {'end': 623.846, 'text': 'we do that using the core node module, FS.', 'start': 619.783, 'duration': 4.063}, {'end': 625.907, 'text': "so let's require that first of all.", 'start': 623.846, 'duration': 2.061}, {'end': 637.956, 'text': "at the top i'm going to say cons fs is equal to require and then the fs module, and down here i'm going to first of all read the file.", 'start': 625.907, 'duration': 12.049}], 'summary': 'Set content type to html, read file and send data as response.', 'duration': 34.963, 'max_score': 602.993, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk602993.jpg'}], 'start': 364.495, 'title': 'Sending html responses and files in node.js', 'summary': 'Covers sending html responses in node.js, including adding html content, replacing default html tags, and demonstrates sending html using node, creating separate html files, and reading/sending data from the html file.', 'chapters': [{'end': 508.661, 'start': 364.495, 'title': 'Sending html response', 'summary': 'Explains how to send html responses in node.js, demonstrating the process of adding html content to the response and how it replaces default html tags, showcasing the ease of sending a response in the context of web development.', 'duration': 144.166, 'highlights': ['The process of adding HTML content to the response and how it replaces default HTML tags', 'Demonstration of sending back an HTML string instead of plain text', 'Showing the ease of sending a response in the context of web development', 'The browser automatically adding the HTML, head, and body tags']}, {'end': 679.274, 'start': 508.661, 'title': 'Sending html using node and reading html files', 'summary': "Discusses the messy way of sending html using node, demonstrates the creation of a separate html file for cleaner code, and explains the process of reading and sending data from the html file using node's file system module.", 'duration': 170.613, 'highlights': ['The chapter discusses the messy way of sending HTML using Node', 'Demonstrates the creation of a separate HTML file for cleaner code', "Explains the process of reading and sending data from the HTML file using Node's file system module"]}], 'duration': 314.779, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk364495.jpg', 'highlights': ['Demonstrates the creation of a separate HTML file for cleaner code', "Explains the process of reading and sending data from the HTML file using Node's file system module", 'The process of adding HTML content to the response and how it replaces default HTML tags', 'Demonstration of sending back an HTML string instead of plain text', 'Showing the ease of sending a response in the context of web development', 'The browser automatically adding the HTML, head, and body tags', 'The chapter discusses the messy way of sending HTML using Node']}, {'end': 1317.446, 'segs': [{'end': 708.146, 'src': 'embed', 'start': 680.706, 'weight': 0, 'content': [{'end': 687.231, 'text': "now, if there's not an error at that point we want to send this data as a response to the browser.", 'start': 680.706, 'duration': 6.525}, {'end': 690.053, 'text': "so let's say else.", 'start': 687.231, 'duration': 2.822}, {'end': 700.66, 'text': 'and inside here we want to say res dot write and we want to pass through the data to that and then we can say res dot end to end the response,', 'start': 690.053, 'duration': 10.607}, {'end': 708.146, 'text': "and it's going to send that data then to the browser, which should be the content of this index.html file now up here.", 'start': 700.66, 'duration': 7.486}], 'summary': 'Send data as response to browser after error check.', 'duration': 27.44, 'max_score': 680.706, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk680706.jpg'}, {'end': 841.287, 'src': 'embed', 'start': 811.045, 'weight': 3, 'content': [{'end': 814.367, 'text': 'dependent on the route that a user visits.', 'start': 811.045, 'duration': 3.322}, {'end': 824.473, 'text': 'so we need a way to figure out the URL that a user requests and, dependent on that URL, send back a different HTML file.', 'start': 814.367, 'duration': 10.106}, {'end': 832.699, 'text': "okay, so the first thing i'm going to do is create another file over here and i'm going to call this about dot html,", 'start': 824.473, 'duration': 8.226}, {'end': 841.287, 'text': "and i'm just going to copy some html from my repo again and paste it in, and this just says about at the top over here.", 'start': 832.699, 'duration': 8.588}], 'summary': 'Developing a system to serve different html files based on user-requested urls.', 'duration': 30.242, 'max_score': 811.045, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk811045.jpg'}, {'end': 1101.785, 'src': 'heatmap', 'start': 1065.188, 'weight': 0.739, 'content': [{'end': 1071.609, 'text': "okay, so we're using this switch statement to figure out what url a user has visited,", 'start': 1065.188, 'duration': 6.421}, {'end': 1076.71, 'text': "and now we're going to take the resulting path and we're going to use it down here to read a file.", 'start': 1071.609, 'duration': 5.101}, {'end': 1083.372, 'text': "so instead of hard coding this, we're just going to pass in the path like so, and that's all there is to it.", 'start': 1076.71, 'duration': 6.662}, {'end': 1086.274, 'text': "that's a simple routing system set up now.", 'start': 1083.372, 'duration': 2.902}, {'end': 1091.838, 'text': 'so if I come over here, in fact we need to cancel out of the process and say node server again.', 'start': 1086.274, 'duration': 5.564}, {'end': 1097.501, 'text': 'and now, if I come over here and go to just the home page, then we should see the home page.', 'start': 1091.838, 'duration': 5.663}, {'end': 1101.785, 'text': 'if I go to forward slash about, then we should see the about page.', 'start': 1097.501, 'duration': 4.284}], 'summary': "Implementing a simple routing system to read files based on user's visited url.", 'duration': 36.597, 'max_score': 1065.188, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk1065188.jpg'}, {'end': 1157.192, 'src': 'embed', 'start': 1131.501, 'weight': 4, 'content': [{'end': 1139.104, 'text': 'So the status code basically describes the type of response being sent to the browser and how successful the request was.', 'start': 1131.501, 'duration': 7.603}, {'end': 1146.648, 'text': "Now, there's many different status codes for loads of different scenarios, but here's some common ones.", 'start': 1139.705, 'duration': 6.943}, {'end': 1149.389, 'text': '200 means that everything was okay with the request.', 'start': 1146.668, 'duration': 2.721}, {'end': 1153.831, 'text': '301 means the resource was moved permanently somewhere.', 'start': 1150.949, 'duration': 2.882}, {'end': 1157.192, 'text': 'And this is to signify a permanent redirect.', 'start': 1154.011, 'duration': 3.181}], 'summary': 'Status codes indicate response type and success. common ones: 200, 301.', 'duration': 25.691, 'max_score': 1131.501, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk1131501.jpg'}, {'end': 1263.531, 'src': 'embed', 'start': 1234.814, 'weight': 2, 'content': [{'end': 1238.417, 'text': "because immediately after this we're ending the response.", 'start': 1234.814, 'duration': 3.603}, {'end': 1245.423, 'text': 'so instead i might want to set a different status code dependent on the type of response for these different things up here.', 'start': 1238.417, 'duration': 7.006}, {'end': 1249.227, 'text': "so i'm going to set the status code in these different cases instead.", 'start': 1245.423, 'duration': 3.804}, {'end': 1253.789, 'text': 'so let me grab that and and let me paste it up here first of all.', 'start': 1249.227, 'duration': 4.562}, {'end': 1258.87, 'text': "so underneath the first case i'm going to say the response.status code is equal to 200,", 'start': 1253.789, 'duration': 5.081}, {'end': 1263.531, 'text': "because in this case they've gone to the home page and they're getting back the home page.", 'start': 1258.87, 'duration': 4.661}], 'summary': 'Setting status code to 200 for home page response', 'duration': 28.717, 'max_score': 1234.814, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk1234814.jpg'}], 'start': 680.706, 'title': 'Handling server responses and sending html pages in node.js', 'summary': 'Explains handling server responses in node.js, including sending data, handling errors, and ending the response. it also demonstrates sending html pages, setting status codes, and explaining their significance such as 200 for successful requests and 404 for non-existent resources.', 'chapters': [{'end': 731.893, 'start': 680.706, 'title': 'Handling server responses in node.js', 'summary': 'Explains how to handle server responses in node.js, including sending data as a response to the browser, handling errors to prevent hanging requests, and ending the response.', 'duration': 51.187, 'highlights': ["The chapter demonstrates sending data as a response to the browser using 'res.write' and 'res.end', ensuring the response is properly completed to prevent hanging requests.", "The chapter emphasizes the importance of using 'res.end' to conclude both successful and error responses in Node.js."]}, {'end': 1317.446, 'start': 731.893, 'title': 'Sending html pages and setting status codes', 'summary': "Demonstrates how to send different html pages based on the user's request, setting up a simple routing system, and explaining the significance of response status codes such as 200 for successful requests and 404 for non-existent resources.", 'duration': 585.553, 'highlights': ["The chapter demonstrates how to send different HTML pages based on the user's request, setting up a simple routing system.", 'Explanation of response status codes such as 200 for successful requests and 404 for non-existent resources.', 'Demonstration of setting different status codes for different types of responses.']}], 'duration': 636.74, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk680706.jpg', 'highlights': ["The chapter emphasizes the importance of using 'res.end' to conclude both successful and error responses in Node.js.", "The chapter demonstrates sending data as a response to the browser using 'res.write' and 'res.end', ensuring the response is properly completed to prevent hanging requests.", 'Demonstration of setting different status codes for different types of responses.', "The chapter demonstrates how to send different HTML pages based on the user's request, setting up a simple routing system.", 'Explanation of response status codes such as 200 for successful requests and 404 for non-existent resources.']}, {'end': 1612.459, 'segs': [{'end': 1416.042, 'src': 'embed', 'start': 1387.579, 'weight': 2, 'content': [{'end': 1393.721, 'text': "now, when they click on those links, people, they're going to go to a 404 page because that no longer exists.", 'start': 1387.579, 'duration': 6.142}, {'end': 1405.136, 'text': 'so it would be much better if i could detect requests to this url so that then i could redirect them to forward slash about.', 'start': 1394.75, 'duration': 10.386}, {'end': 1407.317, 'text': 'so how are we going to do that?', 'start': 1405.136, 'duration': 2.181}, {'end': 1416.042, 'text': 'well, first of all, we need to add in another case into our switch statement right here, and that case is going to be for about hyphen us.', 'start': 1407.317, 'duration': 8.725}], 'summary': 'Redirect requests to /about-us to /about.', 'duration': 28.463, 'max_score': 1387.579, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk1387579.jpg'}, {'end': 1459.12, 'src': 'embed', 'start': 1434.717, 'weight': 1, 'content': [{'end': 1443.225, 'text': "because we're just going to do a redirect and i want to change the status code from 200 to be instead 301,", 'start': 1434.717, 'duration': 8.508}, {'end': 1449.671, 'text': "and that means that the resource you're trying to access has been permanently moved and we're going to do a permanent redirect.", 'start': 1443.225, 'duration': 6.446}, {'end': 1450.512, 'text': "and that's what we're doing.", 'start': 1449.671, 'duration': 0.841}, {'end': 1459.12, 'text': 'right, the about page has been moved from about me to about by me in the past, and so therefore we want to redirect them to this new url.', 'start': 1450.512, 'duration': 8.608}], 'summary': 'Changing status code from 200 to 301 for permanent redirect of the about page.', 'duration': 24.403, 'max_score': 1434.717, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk1434717.jpg'}, {'end': 1527.785, 'src': 'heatmap', 'start': 1484.346, 'weight': 0.818, 'content': [{'end': 1486.207, 'text': "so that's all there is to it.", 'start': 1484.346, 'duration': 1.861}, {'end': 1491.29, 'text': 'we do still need to end the response, and we do that by saying response.end.', 'start': 1486.207, 'duration': 5.083}, {'end': 1498.453, 'text': 'and now, if we cancel out of this process and run it again, hopefully if i go to forward slash about me,', 'start': 1491.29, 'duration': 7.163}, {'end': 1501.535, 'text': 'it will now redirect me to forward slash about.', 'start': 1498.453, 'duration': 3.082}, {'end': 1512.561, 'text': "so let's save that and come out here and go to forward slash about me, and now it redirects us to forward slash about, which is much, much better.", 'start': 1501.535, 'duration': 11.026}, {'end': 1514.872, 'text': 'so then, my friends,', 'start': 1513.931, 'duration': 0.941}, {'end': 1523.3, 'text': 'now hopefully you can see how node works as a server and how it can handle different requests to different routes for different web pages,', 'start': 1514.872, 'duration': 8.428}, {'end': 1527.785, 'text': 'and also how it can send back responses in the form of html pages.', 'start': 1523.3, 'duration': 4.485}], 'summary': "Node server redirects '/about me' to '/about' successfully.", 'duration': 43.439, 'max_score': 1484.346, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk1484346.jpg'}, {'end': 1554.224, 'src': 'embed', 'start': 1527.785, 'weight': 3, 'content': [{'end': 1536.353, 'text': 'now, this way of working right here, using switch and finding out the request url and all that kind of jazz, that is absolutely fine,', 'start': 1527.785, 'duration': 8.568}, {'end': 1543.438, 'text': 'but as your websites get little bit bigger and slightly more complex, for example dealing with many different request urls,', 'start': 1536.353, 'duration': 7.085}, {'end': 1544.819, 'text': 'different types of requests,', 'start': 1543.438, 'duration': 1.381}, {'end': 1554.224, 'text': 'like post or delete requests and database logic and all that kind of stuff then this way of working right here might get a little bit messy and hard to maintain.', 'start': 1544.819, 'duration': 9.405}], 'summary': 'Using switch for request urls may become messy as websites grow bigger and more complex.', 'duration': 26.439, 'max_score': 1527.785, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk1527785.jpg'}, {'end': 1612.459, 'src': 'embed', 'start': 1589.159, 'weight': 0, 'content': [{'end': 1596.726, 'text': 'so we are going to be using the express package to handle all of our routes, requests and responses later on.', 'start': 1589.159, 'duration': 7.567}, {'end': 1607.254, 'text': 'but to do that we need to first understand how to install and use third-party packages, because express is a third-party package.', 'start': 1596.726, 'duration': 10.528}, {'end': 1612.459, 'text': "we need to know how to use those in node and we'll talk about that in the next lesson.", 'start': 1607.254, 'duration': 5.205}], 'summary': 'Using express package for handling routes, requests, and responses in node.', 'duration': 23.3, 'max_score': 1589.159, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk1589159.jpg'}], 'start': 1317.446, 'title': 'Implementing redirects and node server introduction', 'summary': 'Discusses handling 404 errors, implementing redirects, and introducing node.js server redirect techniques, highlighting the significance of redirecting outdated urls and the transition to using express for efficient route and request management.', 'chapters': [{'end': 1434.717, 'start': 1317.446, 'title': 'Handling 404 errors and implementing redirects', 'summary': 'Discusses handling 404 errors and implementing redirects, emphasizing the importance of redirecting outdated urls to prevent users from landing on non-existent pages, potentially resulting in 404 errors, and the process of adding a new case in a switch statement to handle the redirection.', 'duration': 117.271, 'highlights': ['The importance of redirecting outdated URLs to prevent users from landing on non-existent pages and potentially encountering 404 errors is emphasized.', 'The process of adding a new case in a switch statement to handle the redirection of outdated URLs is explained.']}, {'end': 1612.459, 'start': 1434.717, 'title': 'Node server redirect and express introduction', 'summary': 'Explains how to implement a 301 redirect in node.js, followed by an introduction to the use of express as a more efficient way to manage routes, requests, and responses, emphasizing the importance of understanding the underlying concepts in order to prepare for using express.', 'duration': 177.742, 'highlights': ['The chapter explains the process of implementing a 301 redirect in Node.js by changing the status code from 200 to 301, indicating a permanent move of the resource, and using response headers to set the new location, redirecting users to the updated URL.', 'The introduction of Express as a more efficient way to handle routes, requests, and responses is emphasized, indicating the potential complexity of managing multiple request URLs and different types of requests as websites grow, and the importance of understanding the underlying concepts before transitioning to Express.', 'The importance of understanding how to install and use third-party packages in Node.js, specifically highlighting the upcoming use of Express as a third-party package for route, request, and response handling.']}], 'duration': 295.013, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/DQD00NAUPNk/pics/DQD00NAUPNk1317446.jpg', 'highlights': ['The introduction of Express as a more efficient way to handle routes, requests, and responses is emphasized, indicating the potential complexity of managing multiple request URLs and different types of requests as websites grow, and the importance of understanding the underlying concepts before transitioning to Express.', 'The process of implementing a 301 redirect in Node.js by changing the status code from 200 to 301, indicating a permanent move of the resource, and using response headers to set the new location, redirecting users to the updated URL.', 'The importance of redirecting outdated URLs to prevent users from landing on non-existent pages and potentially encountering 404 errors is emphasized.', 'The process of adding a new case in a switch statement to handle the redirection of outdated URLs is explained.', 'The importance of understanding how to install and use third-party packages in Node.js, specifically highlighting the upcoming use of Express as a third-party package for route, request, and response handling.']}], 'highlights': ['Express is emphasized for efficient route and request management', 'Setting up a node server to listen to port 3000', 'Restarting the server after code changes is crucial', 'Logging request details provides user request information', 'Understanding request.url and request.method is crucial for routing', 'Sending different responses based on the route is essential', 'Setting response headers to send specific content type', 'Ending the response properly to prevent hanging requests', 'Creating a separate HTML file for cleaner code', "Reading and sending data from the HTML file using Node's file system module", "Importance of using 'res.end' to conclude responses in Node.js", 'Setting different status codes for different types of responses', 'Introduction of Express as a more efficient way to handle routes and requests', 'Implementing a 301 redirect in Node.js for permanent resource move', 'Importance of redirecting outdated URLs to prevent 404 errors', 'Understanding how to install and use third-party packages in Node.js']}