Node.js and TypeScript: Build a CRUD API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to WD coders today in this video we will create card operation API in nodejs using typescript okay in this video we will use the mongod DB for the database okay so first of all we need to create a blank app you can see my screen in the terminal I'm typing npm in it Hy y so that will be create a blank app and that will generate a package dojon file so some some short of modification we need to do here just like a start I need to add here and instead of this I'm putting no modon okay and we need to install some packages here so uh Express uh yeah Express and uh types Express LPM I Mo noral that also we need to install mongus okay and we need to install typescript mongos okay now okay and the next file we will create uh which we need in the next time okay so some folders We need to cre create uh SRC one file we need to generate TS config uhj TS config.js okay so some uh of the code I'm putting here so you can see what I have done so in the compiler option I have set some uh some lines okay so you can copy this one I will put in the description box also okay and uh now what we need to do is first of all we need to create one folder SRC uh yes SRC and uh here we need to create some folders controllers okay and next one is routes DB okay so one more file I need to create the index.ts file okay index.ts file okay and okay one more thing we need to create one file notb okay Nob dojon so in this file some code also we need to put that also I will put in the description box so that you can copy from there okay so that's it and in the no index.ts file first of all we can check is this application is working or not so log Hello app okay so but before that we need to install typ script also TS node Al we need to install okay yes n PM Run start so you can see Hello app it's working fine now so we need to add uh we need to create application here so Express iting Express from Express okay and uh also we need to import and uh const app Express app.use express. Json this 4,000 this port I am adding here this I want to type one message so already on http Local Host 4,000 okay now we need to uh add the connection for mongod DV okay so one one um database I have been created so that that the same we will use okay const URL so URL will be like this okay now mongus con conect URL DB name we need to provide here NG G okay actually the the same database I want to use um okay let's create the no typescript uh app okay no typescript app this is fine yeah now we need to add then because we need to show one uh message here database connected okay and in the catch we need to add one error message if something will come then we need to show in the log yes like this so this is fine okay so we can uh see now you can see so application is working fine database connected fine so now we need to create some controllers because employ uh API I want to create here so first of all I will create a controller file employ controller. DS okay so I am creating one class employe controller that will be very easy okay now uh we need to add some functions here just like a get all employ okay what we can add here uh we can do like this a sync function we need to add uh respon uh sorry request Express request we can add uh we need to import this Express first request uh response EXP press response okay now we need to add try catch here and con employes a wait employ uh yes one model we need to create so first of all I think we need to create one model in the data phase folder what we need to do is we need to create employ [Music] model. DS okay so and this or employee do model I think we can rename this file employee it's fine okay employee and here we need to create constant employe schema new mongos so mongos will be import automatically okay now we need to add type will be string and required we need to add true okay so I will copy these thing and email we need okay and also unique I want to add here is true okay mobile also I want d o b or d o okay okay dat of joining so employee true this is true this is true I think this is fine I'm not starting true in this this will not be required okay fine um time time I need to add here no to type time strap okay true now export const employ model mongus model model name employ BL schema BL schema okay this is fine now the same thing we need to use here okay wait employ model find okay now or here we can add one return response dot send status is 400 is fine okay now return response status will be 200 and Jon data we need to pass okay employes so one fun has been created now the same thing we need to do uh we need to create for create employee update employee delete employee and one more thing for get also we need so get employe that will be employ and by ID we need to find right so request perms and we can add find by ID we can pass the IDE here and this is also we can do so this is fine get employees also working fine and create employ one more method I'm creating here so I am copy this one okay now the next one what we need to do is we need to uh add body here we need to grab all the data right name email or mobile d o do now we need to create const employ new employee model or we can do one thing name email mobile d o d o so everything have I have been been passed and one I think message we can pass here message employ created okay this is fine I think if we want to pass the data that also we can do just like this okay now so this is also fine uh one more thing employ do save we need to call right and if we have the AIT AIT also we need to add because async we have been added here right so this is also fine now for the update also we need to create so we will make a copy of this one we need to add update here and this ID we need because based on ID we will update the data right so now what we need to do is [Music] uh employ I think not like this we can do uh one thing here employee model find by ID okay we can pass the ID here now so everything should be like this because if we got the data of employee then we will do otherwise we will not do okay here we need to pass updated and otherwise this message oh sorry this one we will send now employ do name name the same thing we uh email mobile do doj wait we need to add here uh d o also mobile email do right so now you can see the upit same message is here so this is fine this is also fine okay now for delete also we need to do something right but before that we need to find the data so I'm copying of this file uh this function I'm changing the name delete employee okay and uh what we need to do is we need a message so now require this one deleted okay so this is I have done now based on I ID uh we can do find and delete this we can do right and what we need to pass here is underscore ID because in the mongod the ID will be like this I'm passing the ID here now this data will be deleted okay employ deleted so this is it now okay for create I think uh yes that will be a 2011 so now all are the functions I have added now we need to work on the routes because routes is very important so what we need to do here is we need to create index. DS one file we need to create first of all we need to import Express Express okay and constant router Express router we need to add okay yes now the router do cat employe employee controller uh first of all we need to export this one export default new employee controller right so now we need the employee controller here and we need get all employee this method we need okay so this is ready based on ID also we can call this one so that will be get employ right now post so create employ what we need to pass the ID also here so that will be update employee now I am copying this one because we need to add the route for deletion delete emply so everything is ready right now we need to export this one export default router right now this one we need to add here app. use and here we can add the router this is it okay now okay one error I think is coming which one so this is saying oh one thing one thing one thing one so everywh we need to add this one actually so this is also we need to add fine f uh not here yes here here also we need to add now let's see yes this is fine and um in the post man we will test this one so you can see everything I have added right so now first of all we can we can call this one get all okay so if we call this one blank blank data we have okay this is fine so first of all I think we need to create the data so one data I have made this so when I click on the set button you can see employee created data also we can see like this okay now you can see when I click on get all right so this is also we have now we need to copy this one okay so based on ID we need to get the data this is also working fine now we need to update the data okay so what we will update so here you can see get employee what we have just like a mobile number we will update okay so we need to do like this not this one I think this we can send Okay so let's check it updated mobile number also coming if we call the cat employee uh mobile number you can see this is updated okay now we need to delete the data so when I click on the send button yeah employee deleted if I click on the get all employees you can see this is gone okay that's it so if you uh like this video just hit the like button and subscribe to my channel uh this is very important guys you are not subscribing my channel so please my request is Please Subscribe my channel for the latest update in the next video These API we will use in angular application okay thank you very much
Info
Channel: WDCODERS
Views: 2,125
Rating: undefined out of 5
Keywords: rest api, typescript tutorial, how to build a rest api, node js api, nodejs, expressjs, express js, mongoose, javascript, mongodb, typescript, using typescript nodejs, nodejs typescript tutorial, rest api tutorial, express tutorial node js, node js api tutorial, node js rest api, node js tutorial, using typescript in nodejs, how to create an api using typescript, restful api node.js, crud express node.js, create api with node.js, node rest api, build api
Id: rmRViU24ypo
Channel Id: undefined
Length: 20min 9sec (1209 seconds)
Published: Wed Dec 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.