create Node JS API with MYSQL database connection

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so first you need node.js install the LTS version of node.js then go to command prompt and check for node version node hyphen V also check for npn version after you have these two in your system then type workbench download click on this link click on go to downloads page download the community version which is a larger file after you download it install this and when it asks you to create a password create root as your password then open workbench so click on this plus icon give your connection a name I'll give it as five connect question click OK open this connection type your password click ok and here click on this create new schema icon so I'll give a name for my schema which is five underscore DB click apply click apply click finish your new schema should be here open this and right click on tables click on create table give your tablet a name I'll give it as five table give our primary key give this as an auto increment for default values you can give some column names column column one tab tab column two tap tap column three okay for default expression I'll have dummy data enter dummy data to end different okay so click on apply apply oh click on apply uh duplicate column two so create this as column three column name should be different then click apply click finish then a new table should be created for you click on this icon so here we have no data so I will type 1 2 three four five okay then I'll click on apply apply finish so our dummy data should be created okay so now copy your note your DB name 5 DB and five table okay DB and table name now close this close everything now create a new folder okay five API okay open this folder in vs code click trust open new terminal and type in pm init hyphen byte this should create a Json file in this Json type type and module okay click save and now we need to install two dependencies so npm install my SQL to install MySQL tool then npn install Express okay so these two dependencies should be installed after these are installed create a new file called index.js here import those two dependencies what my SQL SQL 2. okay and same for the Express Port X press from Express okay now we have to create a new connection so const connection equal to mySQL 2 create connection so this receives an object with the values of our connection to our database so host is localhost localhost it can also be as this IP address like one two seven dot zero dot zero dot one so this is the IP address for localhost and then database so our database is 5 underscore dep so now the user is user is root and password is also root okay in your case it might be different so that's all for our connection and now we create the express app so const app equal to express so this is a function that we just call here so now we give the port 5000. so this is just a variable we'll use this variable now so now in our app we listen to the port so Port 5000 and the second argument is the Callback function so here we'll console DOT log oh server server http colon slash slash local host colon then our port okay so we'll just we are just log in here logging here to the console and then on to our connection we call the connect method okay so this receives a callback function okay so if there is any error in our connection then through error okay so we are just log else so we are logging database connected okay okay this is this is all for listening and now after we listen to our app then we use our app dot use okay we use the we should give now here we give the path on which our API we'll get some data okay so path will be all you can have this as you wish so here this is the second argument distress as a second argument we pass a callback function oh wait so as a second argument we passes callback function quiz once so the default arguments are request and response here for our SQL query SQL query spelling mistake okay we do nothing but select star from our table name so our table name is 5 dot TF table okay so let's start from table that's all and on to our collection bigquery we call the query method and then we pass the query as first argument and the second argument is a callback function so first will be first argument will be error second argument will be result of our query for SQL query okay so if there is any error then through error or else response dot send a result so we are sending the result to the response object okay everything works fine this will send the if data is received from querying this querying this then it will send the result so now what we do is we type the command node index.js so our database is connected everything our API is running on this server so now we click we should see nothing because this URL gives us nothing this when we type all and type enter when we type all and press enter we should see our data so this is a basic API node.js API okay so this is the data in in our database we can check so different data five friend everything is same okay that's all thank you for watching this video see you next time see you next week
Info
Channel: Novel Code
Views: 9,834
Rating: undefined out of 5
Keywords: mysql, workbench, node, node js, javascript, connection, db connection, database, mysql2, express, express js, module, install, workbench mysql, node lts, setup, application, programming, interface, web api, web development, node js api, expres js api, my sql workbench, connect node js with sql, express api with sql db, sequel database, table, schema, create table, create schema, create connection pool, app.use, app.listen, api connection, v8, bun js, common js, npm, js, code js, api, localhost, db
Id: BOhky4ExTN4
Channel Id: undefined
Length: 12min 38sec (758 seconds)
Published: Sun Dec 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.