![]() First, install the nodemon package globally. Let’s use the nodemon module, which will automatically restart our server every time we save a change to the server.js file. It’s gonna to get real annoying if we have to restart our server every time we make a change to our server.js file. The above makes use of the ‘-X’ option we can pass curl to GET or POST to an endpoint. authTut $ cd client client $ cURL -X GET you just hit the home page Then, call the cURL command and pass in some options to get our homepage endpoint. Open up a new tab or window in your terminal and change into the /client folder. Now, when you revisit the you should see the ‘you just hit the home page’. Listening on localhost:3000 ^C server $ node server.js Listening on localhost:3000 We can do this by pressing ‘control C’ while in the terminal and then running ‘node server.js’ again. We need to go to ahead and restart the server after saving our changes. If you go to now, you will still see the ‘Cannot GET /’ error, because our old file is still acting as the server. The ‘req’ and ‘res’ parameters handed to our app.get(‘/’) callback function are the ‘request’ and ‘response’ objects generated from the request headers that came in. ![]() Open the /authTuts folder in your favorite text editor, then open the authTuts/server/server.js file.įirst we are going to require the express module, then we call the express() function to create our app, and lastly we tell express which port to run on. After calling this function, you should see the defaults options logged to the console.Īt this point you should have a folder/file structure that looks like the following: - /authTuts - /server - /node_modules - server.js - package.json - /client Step 3) Create the server and run it Note, passing the ‘-y’ flag to ‘npm init’ automatically accepts the defaults that npm initializes our project with. authTut $ cd server server $ npm init -y server $ npm install express -save server $ touch server.js Next, go ahead and install express as a dependency, then create a server.js file. workspace $ mkdir authTut workspace $ cd authTut authTut $ mkdir server authTut $ mkdir client Step 2) Initialize npm and install express in the /server folderįirst, we change into our /server folder, then initialize npm, so we can keep track of what dependencies our server has. Run the following commands in your terminal. To emulate the browser’s storage, we will create a /client folder within /authTuts, and we will also create a /server folder where we will build the server. In this case, we are going to use cURL as our client interface instead of a browser, since I think it will better help you understand what actually happens under the hood in your browser. Step 1) Set up the file structureįirst, we’re going to create a top-level folder called “authTut” just to hold the 2 sides of the project, the server and the client. This tutorial assumes some familiarity with the terminal/command-line interface (CLI) and Javascript / Node.js. This tutorial/explainer is meant to actually walk you through the authentication process and explain each mechanism. ![]() Instead of actually explaining the mechanics and what’s going on, I just feel like the author is simply providing a walkthrough of how to copy/paste from the docs. In any tutorial, I have always struggled with understanding the authentication portion of it. Node.js Server & Authentication Basics: Express, Sessions, Passport, and cURL
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |