![]() To keep it simple, we’re going to tell our server to respond with plain text, by using the setHeader method. #Quick node server setup how to#The request and response parameters do exactly as implied - they handle the “request” to, and the “response” from, our server - so let’s tell our server how to respond after its been requested. Http.createServer((request, response) => ) We’re going to pass another function into createServer with a “request” parameter, and a “response” parameter, as scene in the following code snippet: Now that we have access to the HTTP module, we can utilize the createServer function (using JavaScript dot notation) - which does just at it sounds, it creates a new server for us. In this case we’re going to be using the HTTP module that Node.js has built for us, and we can access it using the built in require function by adding the following line of code to the top of our JavaScript file: If you would like to learn more about what modules are available to you through Node.js, or how to use a specific module, you can check out their API Documentation.Ī Javascript module is essentially a bunch of functions written out that we can access and reuse over and over - saving us the time of having to write the code ourselves. When you install Node.js, it comes with quite a few JavaScript modules that we can use to our advantage. You can do this by creating a new file directly from your code editor, or you can head back to your terminal and create one from there using the command touch server.js Now, let’s add a new JavaScript file to this directory so that we can write some code. Creating a new directory named “nodejs-server”, checking that it was created using the “ls” command, switching into that new directory, and then opening it in VS Code.Īt this point, you should have your newly created - and empty - directory open in your code editor.įrom your terminal, you can use the command pwd to check your “present working directory” if you’re unsure what directory you’re in. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |