Restful API with NodeJS, Express & Typescript [2020]

  Рет қаралды 46,235

The Nerdy Canuck

The Nerdy Canuck

Күн бұрын

Пікірлер: 135
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
Hey guys! Thanks for watching. Just a small FYI: I missed an important part of logging, in the current example, the logging on the server will only log the req before the response is returned, so I fixed it in the repo to listen for the response to end. Please take a look at: github.com/joeythelantern/Typescript-Quickstart-Projects/blob/main/typescript-mysql-quickstart/source/server.ts
@anselmleo4146
@anselmleo4146 3 жыл бұрын
Nice.. I was wondering why you didn't go with Winston or other loggers out there. Is this the best approach with Typescript?
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
@@anselmleo4146 Nope, just an easy way to do it on your own.
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
Hola, también estoy usando un traductor. Muchas gracias, estoy feliz de que te gusten mis videos.
@ProximityGM
@ProximityGM 3 жыл бұрын
Why do you want the request to log after the response is returned? I don't see how this is a mistake. Can you please explain?
@brennonwilliams9181
@brennonwilliams9181 2 жыл бұрын
Seriously, one of the best tutorial videos I have seen in a very long time. Your delivery of the content is perfect because you're not showing things that make you look smart and nobody needs or the context is a distraction - you're showing exactly the content needed, with a great cadence, enough detail without getting stuck in the weeds, and showing a logical path to constructing the solution. Well done sir.
@janlestermallari
@janlestermallari 3 жыл бұрын
This is so far the *EASIEST* and *STRAIGHTFORWARD* TS setup I've ever watched. Thanks for this short tutorial.
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
No problem. I goal is always to be thorough and as accurate as possible.
@johnnydriesen7575
@johnnydriesen7575 3 жыл бұрын
This channel is far underrated. Thanks for this great vid, @Canuck.
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
No problem! It's not even that it's underrated, I've just been away from KZbin for a long time and couldnt concentrate on it before. Hopefully my releasing of a video once or twice a week from here on out brings people back :)
@IkraamDev
@IkraamDev 3 жыл бұрын
First time I’ve seen someone do logging like this in a tutorial.
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
Is that a good thing?
@IkraamDev
@IkraamDev 3 жыл бұрын
@@TheNerdyCanuck Yeah, it's something different.
@NikhilAdiga
@NikhilAdiga 3 жыл бұрын
Yes! Usually people use libraries like Morgan. This is something different.
@PovilasCiplis
@PovilasCiplis 3 жыл бұрын
very efficient, no bs, straight to the point. loving this. see you at 100k subscribers very soon.
@TheNerdyCanuck
@TheNerdyCanuck 2 жыл бұрын
I hope so
@derik4622
@derik4622 3 жыл бұрын
Had such a fun time with this! Really great content!
@walid7189
@walid7189 3 жыл бұрын
This is one of the best tutorials I have ever come across. Thanks.
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
No problem! Like and share my friend :)
@dahutt
@dahutt 3 жыл бұрын
Thank you for this! I've been beating my head at the wall for the past week trying to learn how to set something like this up this and this finally did it.
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
I got u!
@sivak7183
@sivak7183 3 жыл бұрын
This is the best explanation I have ever found, Keep going well done.
@AnsiString
@AnsiString 2 жыл бұрын
Just awesome! That was exactly that, I was looking for. Thousand thanks to you. Greetings from Germany.
@mishasawangwan6652
@mishasawangwan6652 3 жыл бұрын
first: thank you for taking the time to share your knowledge. it’s clear from the comments this video is helpful for many just getting started. and that’s great but .. the video title is misleading: there’s basically no usage of typescript in this video. yes there are maybe 2 examples where you use type hints in some function signatures but aside from that, this is just vanilla javascript.
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
That's because for a basic example like this, there isn't much difference
@Jason-yr6fy
@Jason-yr6fy 2 жыл бұрын
Do you have adjusted or know a template that uses more typescript? Would be interesting to see :)
@ulana1189
@ulana1189 3 жыл бұрын
Clear, nice and informative. You should make proper video tutorial with React+Redux, Node+Express+Mongo with typescript. I am sure it would be one of the best tuts. Looking forward 😎
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
I'm actually doing on with mongo right now, should be out within a few days 🤓
@ulana1189
@ulana1189 3 жыл бұрын
@@TheNerdyCanuck cool👍👍👍
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
@@ulana1189 It's up, let me know what you think!
@djmous5367
@djmous5367 2 жыл бұрын
Well organized and to the point. How can I have VSCode stop at breakpoints? extensions needed? launch.json configuration? please specify or point me to an actual example. Thank you.
@phamvanhan7037
@phamvanhan7037 3 жыл бұрын
The best video I could found! Thank you.
@madhand1472
@madhand1472 2 жыл бұрын
great video! could you explain soft delete with node js
@RushabhWadkar
@RushabhWadkar 3 жыл бұрын
This was so great. Perfect! Exactly what I needed.
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
:)
@juanchox0929
@juanchox0929 3 жыл бұрын
What an excellent explanation!! Thanks for sharing your knowledge, I'll put it on practice right now
@supa1009
@supa1009 3 жыл бұрын
Bro you are a legend, like really thank you for your help!
@KuRoiBatusai
@KuRoiBatusai 3 жыл бұрын
Hello, looking for a node + typescript tutorial, I found your video, by the way TOO good, I am a Spanish language, so if something is a little strange it is because of the translator ajja, this time I wanted to write more than necessary, because I see that you Programming logic is similar to mine, that's why I loved your video a lot and I always wondered can I do this, how is it done, and you just leave your log for future use for monitoring (DevOps), etc. Then I see the github and you have the same thing in mongoose, I learned more things by watching your video and you earned a subscriber, continue like this, good material and now I'm watching all the videos, thanks for your hard work. By the will it was that he used a translator. Greetings from Chile
@KrisFoster1
@KrisFoster1 3 жыл бұрын
Great video, thank you! Any reason why you don't use a popular middleware logging package like morgan?
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
You can totally use any logger you want! I prefer to do simple things myself to not bloat my libraries, but a logging library wouldn't really bloat anything. It's just personal preference on small projects :)
@tamarperetz9641
@tamarperetz9641 3 жыл бұрын
Thank you so much! really informative and helpful
@SuperZartok
@SuperZartok 3 жыл бұрын
Thanks a lot for this tutorial
@leonardo.cardoso
@leonardo.cardoso Жыл бұрын
really good tutorial!
2 жыл бұрын
do you have a continuation of this tutorial on how to store stuff on mongo or something?
@TheNerdyCanuck
@TheNerdyCanuck 2 жыл бұрын
Yes, if you go to my recent uploads, I have a video similar to this about mongo.
2 жыл бұрын
@@TheNerdyCanuck Thanks
@giovannipizzato6888
@giovannipizzato6888 2 жыл бұрын
Dude you are awesome, thanks!
@TheNerdyCanuck
@TheNerdyCanuck 2 жыл бұрын
no you are!
@ShinYo627
@ShinYo627 3 жыл бұрын
Can you elaborate the purpose of logging functions from /source/logging bit more please? I get that they are reusable functions that are supposedly used for loggings when having different situations like logging infos inside error handlers or just infos on every request.... (hope I'm getting right so far here) . Then, how can you use the debug function from logging.ts in future? I'm just asking because it's just fascinating to find some different working method/pattern than I'm used to.
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
I use logging like this as it's lightweight and I'm just interesting in console logging, however you could expand those functions for sure
@SpencerDavis2000
@SpencerDavis2000 3 жыл бұрын
great tutorial thanks
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
No problem!
@isidme
@isidme 3 жыл бұрын
Awesome tutorial, thanks.
@jotube6204
@jotube6204 3 жыл бұрын
Hey Nerdy Canuck, your tutorials a awesome. How can I contact you for further payed support? Greetings Jo
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
How can I help you? What kind of support do you need.
@aleksandarhristov2918
@aleksandarhristov2918 3 жыл бұрын
Great tutorial. Much appreciated!
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
No problemo!
@manthanpatel779
@manthanpatel779 3 жыл бұрын
Love this Tutorial
@prakharkapoor2572
@prakharkapoor2572 3 жыл бұрын
how i deploy build folder to heroku or somewhere else?pls make a video on it
@TheNerdyCanuck
@TheNerdyCanuck 2 жыл бұрын
I have made a video for you
@rushabhmehta9458
@rushabhmehta9458 3 жыл бұрын
Amazing video, thanks - I'll admit I did a double take at const router = express(); -> much more used to people doing app = express();
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
Yeah me too! Even when I name it router sometimes I do the same thing haha!
@AnkurJhavery
@AnkurJhavery 2 жыл бұрын
My node installation never gives a summary after installing packages like shown in the video: body-parser@1.19.0 + express@4.17.1; how do I get this?
@TheNerdyCanuck
@TheNerdyCanuck 2 жыл бұрын
Do you want specific versions or more console output?
@Nico-zl8cf
@Nico-zl8cf 3 жыл бұрын
great tutorial, thank you!
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
no problem :) .. Any other tutorials you would like to see?
@ashiqsultanmohamed9765
@ashiqsultanmohamed9765 3 жыл бұрын
Can I know why you are not using a pre build npm package for logging? like Morgan
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
Just personal preference, you can use any logging you want
@CalumNoade
@CalumNoade 4 жыл бұрын
So helpful, thank you!
@donmikele07
@donmikele07 3 жыл бұрын
Recommendations: /** Packages, configs, utils */ import express from 'express' import logging from './config/logging' import config from './config/config' /** Import controllers */ import errorController from './controllers/errorController' /** Types */ import { Application, Request, Response, NextFunction } from 'express' /** Constants */ const NAMESPACE: string = 'Server' const app: Application = express() const PORT: number | string = config.server.port /** Logging the request */ app.use((req: Request, res: Response, next: NextFunction) => { logging.logging(NAMESPACE, req, res) res.on('finish', () => { logging.logging(NAMESPACE, req, res, true) }) next() }) /** Parse the request */ app.use(express.urlencoded({ extended: false })); app.use(express.json()) /** Routes */ /** Error handling */ app.use(errorController.get404); app.listen(PORT, () => console.log(`Server running on the port ${PORT}`)) const NAMESPACE: string = 'logging' const logging = ( namespace: string, req: Request, res: Response, status: boolean = false, logType: string = 'INFO', object?: any ): void => { const logInfo = lagData(namespace, req, res, status, logType) ..... }
@princekelsey2931
@princekelsey2931 4 жыл бұрын
love this. Thanks
@TheNerdyCanuck
@TheNerdyCanuck 4 жыл бұрын
Thank you! Let me know a topic you'd like me to cover in the future!
@IkraamDev
@IkraamDev 3 жыл бұрын
Would you keep the logging in production?
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
In production, I would log to a file using a log library that has some sort of log rotation.
@spottedZebra07
@spottedZebra07 2 жыл бұрын
Hi, what is the resolution of your monitor? Video looks crisp on mine at 1440p
@TheNerdyCanuck
@TheNerdyCanuck 2 жыл бұрын
I do my videos in 2K (1440p)
@d-one-and-only
@d-one-and-only 3 жыл бұрын
Just wondering why would you split up the controller and the rouites into two separate files? Would it have not been a bit more unified if the 'controller' would include the router and export the router?
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
When API's get large it's better to have things separated as when you scale up it can get chaotic in the code. That's my personal opinion anyways, I like to compartmentalize things.
@usmanmughal5916
@usmanmughal5916 3 жыл бұрын
Hey use typegoose with typegraphql
@mehmetaltinsoy525
@mehmetaltinsoy525 4 жыл бұрын
Nice video.Thanks.
@TheNerdyCanuck
@TheNerdyCanuck 4 жыл бұрын
No problem! Please like and share :)
@СаидТашманов
@СаидТашманов 3 жыл бұрын
Спасибо!
@TheNamesJT
@TheNamesJT 3 жыл бұрын
This timestamp section is out of my scope of understanding what hes doing 10:50 do you have a video teaching headers, responce and request? like the rules of the api commented section would have never of known to do that. Even tutorials teaching restapi don't include the header rules. Update: Alright 18:36 can't seem to run server.ts it says my logging.info isn't a function on line 50 also added a .env file and added those 2 variables in it SERVER_PORT - SERVER_HOSTNAME and then deleted the config.ts script and just added process.env.SERVER_HOSTNAME and the other one to the functions that was using the config because for some reason was getting undefined for my env's so that fixed those error but now my logging functions aren't being recognized as functions line 50 even though everything is spelt right and did exactly what you did at 18:35
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
I find that if I'm having issues with ENV vars I just hard code them first in the config (without using dotenv) to make sure they are working properly first. Once I get the API running, then I would go back and try and fix em.
@TheCstriker
@TheCstriker 3 жыл бұрын
Also, maybe I'm wrong (as I'm not a backend developer but trying to learn), but shouldn't next be called on the sampleHealthCheck controller function?
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
The reason we don't call next is because that function is the last stop before we return a response to the user. Once you return a response, the next function has nothing to pass the request to.
@TheCstriker
@TheCstriker 3 жыл бұрын
@@TheNerdyCanuck so that means only middleware functions should call next in order to cascade into each other right? And if that’s true, can’t we just don’t declare that parameter and live with req and res only?
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
@@TheCstriker Correct. Next is used if you need to continue with the request. Technically you could not declare 'next' on the function, but I like to put them in because I think it's good coding practice to always know the parameters available to you even if you are not using them. :)
@newstuff0629
@newstuff0629 3 жыл бұрын
design code so great men, thanks!
@johnkarippery9919
@johnkarippery9919 2 жыл бұрын
why i get this error after npm run build > api@1.0.0 build > rm -rf build/ && prettier --write source/ && tsc 'rm' is not recognized as an internal or external command, operable program or batch file.
@TheNerdyCanuck
@TheNerdyCanuck 2 жыл бұрын
Thats because you aren't running it on a compatable terminal. rm -rf is meant for bash. If you are using powershell or something like that, you have to change it to that terminals command
@aram5642
@aram5642 2 жыл бұрын
I find coding to be not as much challenge as getting eslint to work in vscode the way I want. What a nightmare!
@josuequinteros3789
@josuequinteros3789 4 жыл бұрын
thanks, so time booster
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
No problem! If you have any requests let me know :)
@spaghettispaghetto
@spaghettispaghetto 4 жыл бұрын
great video
@TheNerdyCanuck
@TheNerdyCanuck 4 жыл бұрын
Thanks. Any specific topic you want covered in the future?
@hiepnguyen1774
@hiepnguyen1774 3 жыл бұрын
amazing, good job (y)
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
No problem! If you have any requests let me know :)
@tauseefanwardo
@tauseefanwardo 2 жыл бұрын
why do your req, res and next not asking for type, as we are developing in typescript in the server.ts file?
@TheNerdyCanuck
@TheNerdyCanuck 2 жыл бұрын
When using a router, express automatically injects them / knows the types. When you define it separately, then you must define them like in the controller
@tauseefanwardo
@tauseefanwardo 2 жыл бұрын
@@TheNerdyCanuck Good Stuff! Thanks!
@nect3276
@nect3276 3 жыл бұрын
can someone tell me why i get always an empty json back
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
What does your controller function look like?
@touhidulshawan96
@touhidulshawan96 3 жыл бұрын
How to logout user from this implementation?
@TheNerdyCanuck
@TheNerdyCanuck 2 жыл бұрын
No need for this implementation
@zainjahangir296
@zainjahangir296 2 жыл бұрын
hi im facing an error when i try to run the server Unknown file extension ".ts"
@TheNerdyCanuck
@TheNerdyCanuck 2 жыл бұрын
What command are you using
@tristheflash6928
@tristheflash6928 3 жыл бұрын
this is the frist i try with node js and express js , i feel config really complex with me :(
@TheNerdyCanuck
@TheNerdyCanuck 2 жыл бұрын
What can I do to help?
@roshanchauhan2057
@roshanchauhan2057 3 жыл бұрын
Getting error while build the app "Error: Cannot find module 'express'
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
What folder are you trying to run from? Did you import at the top?
@roshanchauhan2057
@roshanchauhan2057 3 жыл бұрын
@@TheNerdyCanuck I run this folder and command nodemon source/server.ts
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
@@roshanchauhan2057 perhaps you forgot the type definitions?
@sourabhvaishnav8235
@sourabhvaishnav8235 3 жыл бұрын
app.use(cors()); could have shorten the video by a minute.
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
Probably
@siphenathindabazonke8895
@siphenathindabazonke8895 3 жыл бұрын
why is it complaining about import statement outside a module and pointing to the HTTP import?
@TheNerdyCanuck
@TheNerdyCanuck 2 жыл бұрын
Where is the error in your code (paste it here)
@dharmarajr24
@dharmarajr24 3 жыл бұрын
Ts-node 😭
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
Yeah I've had my issues with it as well haha
@dharmarajr24
@dharmarajr24 3 жыл бұрын
@@TheNerdyCanuck Finally managed to run my node server. The issue was 'tsc src/index.ts' wasn't running.. but only 'tsc' did. Maybe specifying filename ignores tsconfig
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
@@dharmarajr24 you mean in your package.json file? Yeah if index is in there it'll fail 🤦
@IPhonixI
@IPhonixI 3 жыл бұрын
npm run build > api@1.0.0 build D:\Projects\Routin\typscript-express-nodejs-quickstart > rm -rf build && prettier --write source/ && tsc 'rm' is not recognized as an internal or external command, operable program or batch file. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! api@1.0.0 build: `rm -rf build && prettier --write source/ && tsc` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the api@1.0.0 build script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\MyCroft\AppData\Roaming pm-cache\_logs\2020-12-12T19_40_12_114Z-debug.log
@TheNerdyCanuck
@TheNerdyCanuck 3 жыл бұрын
What shell are you using?
@IPhonixI
@IPhonixI 3 жыл бұрын
@@TheNerdyCanuck VSCode Powershell
@IPhonixI
@IPhonixI 3 жыл бұрын
@@TheNerdyCanuck i tested on git shell too
@IPhonixI
@IPhonixI 3 жыл бұрын
$ npm run build > api@1.0.0 build D:\Projects\Routin\typscript-express-nodejs-quickstart > rm -rf build && prettier --write source/ && tsc source\config\config.ts 307ms source\config\logging.ts 62ms source\controllers\sample.ts 23ms source outes\sample.ts 7ms source\server.ts 50ms Version 1.0.3.0 Syntax: tsc [options] [file ..] Examples: tsc hello.ts tsc --out foo.js foo.ts tsc @args.txt Options: --codepage NUMBER Specify the codepage to use when opening source files. -d, --declaration Generates corresponding .d.ts file. -h, --help Print this message. --mapRoot LOCATION Specifies the location where debugger should locate map files instead of generated locations. -m KIND, --module KIND Specify module code generation: 'commonjs' or 'amd' --noImplicitAny Warn on expressions and declarations with an implied 'any' type. --out FILE Concatenate and emit output to single file. --outDir DIRECTORY Redirect output structure to the directory. --removeComments Do not emit comments to output. --sourcemap Generates corresponding .map file. --sourceRoot LOCATION Specifies the location where debugger should locate TypeScript files instead of source locations. -t VERSION, --target VERSION Specify ECMAScript target version: 'ES3' (default), or 'ES5' -v, --version Print the compiler's version: 1.0.3.0 @ Insert command line options and files from a file.
@IPhonixI
@IPhonixI 3 жыл бұрын
it could not create build folder
Restful API  with NodeJS, Express, Typescript & Mongo / Mongoose [2020]
16:10
Typescript API in NodeJS / Express in Depth [Part 1]
38:10
The Nerdy Canuck
Рет қаралды 12 М.
这是自救的好办法 #路飞#海贼王
00:43
路飞与唐舞桐
Рет қаралды 119 МЛН
When u fight over the armrest
00:41
Adam W
Рет қаралды 22 МЛН
Random Emoji Beatbox Challenge #beatbox #tiktok
00:47
BeatboxJCOP
Рет қаралды 58 МЛН
How to build a REST API with Node js & Express
58:40
Programming with Mosh
Рет қаралды 1,7 МЛН
RESTful API using Node JS, MongoDB & Typescript IN-DEPTH [2022]
49:06
The Nerdy Canuck
Рет қаралды 56 М.
How to setup Nodejs Express with Typescript
28:20
Mafia Codes
Рет қаралды 18 М.
Learn Express JS With TypeScript
40:16
TomDoesTech
Рет қаралды 75 М.
I tried 8 different Postgres ORMs
9:46
Beyond Fireship
Рет қаралды 434 М.
Clean Architecture in TypeScript
58:19
Donny Roufs
Рет қаралды 25 М.
Build A Restful Api With Node.js Express & MongoDB | Rest Api Tutorial
53:55
这是自救的好办法 #路飞#海贼王
00:43
路飞与唐舞桐
Рет қаралды 119 МЛН