Create Node js API with SQL server || Use SQL Query || Use Stored Procedure #nodejs #nodejstutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so today we will create node.js API with SQL Server so we will perform crowd operation so let's start if I have write down some steps in my notepad file will follow all the steps and I will also write the code to save some time and I will explain the each file use so let's move to our step one create one folder with your application name okay go to file path and type CMD and hit enter Then command prompt will be open with your application okay so I've already created the folder with my application like node API correct with SQL so I will go to CMD okay now I will open code space dot so it will open the visual studio code okay so next step now run this command npm init hyphen hyphen y so basically this will create the package.json file and now open Terminal so okay if I write down this step from the command prompt so you can perform this command from the command prompt and also you can perform this command from the visual studio code terminal so let's open that terminal m in it hyphen iPhone y or you will see that it has created package.json file okay so next is npm install so I will install run this command later so next is NTM install Express Ms SQL course body parts over so it will install Express package MSS School package codes for to stop the course error and body parser so body parser will be helpful in Json let's copy this command so you can see that it has created the node module folder so added 170 pack 173 packages now install npm install FS extra so FS extra basically used for file streaming like to read the files so we will use this package for reading the SQL file so I will create some SQL file in our project to write the SQL statements next is npm install nodbone for not Bond basically used for like two initialize your start file and other things so you can like read about the Northbound in detail on the Google also now I will run the npm install okay so it is saying up to date so that's why I've created this command so I have already installed the old packages and then I'll try to run npm install so next step is and now go to script section in the package.json file and add the line start not more index.js so basically this will this will like helpful to run the index.js page so go to this section script so this is our initializing Point means your application will be start from the index.js so means we have to create one index.js file so I'm creating index.js okay now run this query npm install.net environment so this dot dot environment sorry dot dot net my bad so npm install dot environment so it will create the daughter but basically I will create a DOT EnV files or keep the all environment settings like what is the database name hostname on which server localhost will run so I will create create all folders controllers route data so let's create the folders structure alerts out sorry [Music] third one is data and now now create one file with the name dot EnV so basically environment variable so in environment variable in node are used to store sensitive data such as password API credential and other information that should not be written directly in code okay so dot environment file when you like publish your code or when you build your course so that file will never be like readable so that's why we keep all sensitive information in environment file so let's create one file dot EnV o as you can see that this is a setting symbol so the symbol is telling the all uses of this file so next step now create another file with the name config.json okay config.js okay now write the block code in dot EnV file so I will copy all the code and paste into this so you can see that I have used the port 8081 host localhost and host URL is localhost 8081 so this is my SQL Server credentials username password my database name and that this app by IP address so you can find this IP address in your computer management so let me show you where you can find this go to computer management into service and application SQL Server go to SQL Server network configuration protocol for MSS conserver so remember this point that you should or it should be enabled tcpip should be enabled if this is disabled please enable this so go to IP address so I will use the latest version of Ip ip12 is the latest version in my machine so I will copy this and paste into my environment file so as you can see I've already now write the block code to config.js okay we have use the dotnet environment area set and we are we have created one course Port host first URL user password is called database so which settings we have written in this environment variable so we have already used in conflict.js so it will export the all things now create one file with the name index.js in project so I've already created the index.json now create route file inside the route folder with the name my router you can choose the name as per your requirement or as per your so let me hit one file so my route file will be helpful to routing the all urls so if your API endpoint URLs so you can write as per your name into my account so we will take the example all right so in this file we have created the we are using the express version and we are using the controller so user control I will create it later so these are the methods which I will use so I have copied this from my existing code we are not using the other ones so I'm writing the get tool user so this will be the your API URL so in the environment file index.js sorry environment file so once you will hit this URL with your um endpoint URL your API will be hit so there is one more point I will press this vector so in the index.js I will use this code index.js so I was talking about the API URL so your url of API will be look like this and this and plus and here is my route file yes so this will be the URL for that method get all user get all user I will create that method in my index and controller file so this will be the your so step number 16 create a file your test.js inside the data folder to delete files like SQL file which will be used for DB query so let me create the utils file it is basically like utility support so I was talking about that we have to use the we have to read the some files of SQL so I will create the SQL file in my data folder so like abc dot SQL so in the abc dot SQL I will write like select statement delete update or insert Etc so basically this code will help to read the SQL so you can if this is like a static code nothing as a logical code now create one folder in the under data folder for your old SQL file and operation like users I will create one folder users so I will create the old SQL file under the user so I am using the like user database so that's why I keep this name as users you can choose as per your requirement now create all SQL file under the user folder and write SQL query in the file like that update related example control user dot SQL so let me create one SQL file so as you can see that the symbol is for database so let me write some experiment let's start from kbl user okay so what is the next step create one more file with the name index.js under the user folder okay next.js okay we will write all DB operation here in index.js file okay so basically we are using the utility file and config file and mssful servers package so we are requiring these three things and I have created one method get user so basically we are connecting with the SQL server and like we are loading the all scripts which are under user folder so pull dot request basically we are using the user list so it will have to read the data so maybe format so pull dot request.query so query what will be the query SQL queries dot get all user so this get one user dot SQL file name inside the data user folder so I created this file so this is the file name of your SQL statements so it will return the user list dot records so now create One controller file okay inside the controller folder .js and write the block code so I'm performing the controller model it views you can see so this is our controller file so I am requiring the data and data slash user folder so uh in user folder it will require the index.js or any JS file so it will connect with that so I've created one more one method same name or you can choose the name as period permit so I'm saying that user data dot get all user get user sorry if I will create go to definition so it will take me to index.js you can see okay so it will respond dot send user list so which data will come it will send to the response and if any exception will be there so it will give the 400 error and the error message and I will export the get all user method suppose if I have one another method of this delete all users oh I have to write in this way okay foreign so let's move to New terminal let's start oh sorry I have to run the npm start [Music] so as you can see that it has started the index.js starting not inductor case in exit waiting for changes before they start means your file is waiting for next it changes or let me go to environment file and I think I have already written the method name and go to so I think we are missing something thing apism let me check the URL again sorry [Music] okay so I think index.js I have to write dot lesson conflict okay so this is optional you can comment it out no terminals art okay I'll go to this [Music] okay okay so you can see that our select all method is working so let me insert one more row [Music] hello .com [Music] okay great so in the similar way I will create the whole uh method like delete update insert so I've already created one application with the same grid operation so let me open that application [Music] happens so as you can see that I've already read the create query delete get user by ID did very and if we will jump to purchase so I've already read all methods over there so if you don't want to create these files inside the user folder and if you want to use the store procedure so this is the way you can write the show there in this way so this your SQL parameters like uh what are the stored procedure parameters you can write in this so let me take to the create user example so in the create user let me open the foreign so insert into table username email values and these are the parameters and it will return the scope identity we are translated row okay okay so these are the way of sending the parameters into this SQL query so the parameter so this will be your request parameter so let me take to user controller on the create user method so these are the way request response and next the request I have already write the request.body so and I pass this data constant to get user method okay so I will share this code okay thank you
Info
Channel: Education with Ankur
Views: 6,961
Rating: undefined out of 5
Keywords: How to, how to create node js application, how to create node js api, how to create node api, create node api, how to sue sql server in node js, use sql server in node api, how to use ms sql in node js, node js api with sql server and use stored procedure, crud operation in node js api, use stored procedure in sql server in node js api, use cors setting in node js api, node js tutorials, educationwithankur, how to insert data in database using node js api, mssql in node js api
Id: ErK3Qt52a1M
Channel Id: undefined
Length: 21min 51sec (1311 seconds)
Published: Sun Feb 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.