TypeScript and NodeJS: The Proper Setup!

TypeScript and NodeJS: The Proper Setup!

Josh tried coding

2 года назад

53,580 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@ballonura
@ballonura - 04.12.2022 14:48

Why dir called "server" and no "src"? (naming best practice)

Ответить
@okage_
@okage_ - 05.12.2022 10:17

do you have a link to your github or is it private

Ответить
@piiaall
@piiaall - 19.03.2023 18:30

Your explanation is very clear

Ответить
@ponchobob
@ponchobob - 07.06.2023 13:36

maybe for some others here a simple or stupid question, but is nodejs used in big companies for their backend? or is this just some sort of "hobby backend library" ?

Ответить
@WarframeCrunch
@WarframeCrunch - 20.06.2023 21:54

I don't know why I'm getting this error :/

\middleware\errorHandler.ts:45
export default errorHandler;
^^^^^^

SyntaxError: Unexpected token 'export'

My server.ts file:
const express = require("express");
const errorHandler = require("./../middleware/errorHandler");
const dotenv = require("dotenv").config();

const app = express();
const port = process.env.PORT || 5000;

app.use(express.json());
app.use("/api/contacts", require("./../routes/contactRoutes"));
app.use(errorHandler);

app.listen(port, () => {
console.log(`Server running on port ${port}`);
})

and

errorHandler.ts file:
import {Request, Response} from 'express';

type typeErrorHandler = {
err: {message: string, stack: string[]},
req: Request,
res: Response,
next: string,
}

const errorHandler = ({err,req,res,next} : typeErrorHandler) =>{
const statusCode = res.statusCode ? res.statusCode : 500;

switch (statusCode) {
case 400: res.json({
title: "Validation Failed",
message: err.message,
stack: err.stack,
})
break;

case 401: res.json({
title: "Un authorized",
message: err.message,
stack: err.stack,
})
break;

case 403: res.json({
title: "Forbidden",
message: err.message,
stack: err.stack,
})
break;

case 404: res.json({
title: "Not found",
message: err.message,
stack: err.stack,
})
break;

case 500: res.json({
title: "Server Error",
message: err.message,
stack: err.stack,
})
break;

default:
console.log('No error, all good.');
break;
}
}

export default errorHandler;

Ответить
@scorcism.
@scorcism. - 10.07.2023 14:32

subscribed

Ответить
@greggsworld4453
@greggsworld4453 - 26.07.2023 21:37

This was great! Thank you so much. It would be great if you added server restart on code changes to avoid having to start and stop - like nodemon.

Ответить
@ilearncode7365
@ilearncode7365 - 07.08.2023 04:15

how does tsc still work after you moved it into a folder? That doesnt make sense.

Ответить
@kurtking4662
@kurtking4662 - 20.08.2023 14:44

Enjoyed watching your video!

Ответить
@nomadshiba
@nomadshiba - 28.08.2023 13:40

tsx (package, not react's tsx), is better than ts-node
it just always works
even lets you execute .ts files directly with this hashbang `#!/usr/bin/env tsx`

Ответить
@one_quy
@one_quy - 04.09.2023 09:15

gooodddd, thank youuuu

Ответить
@NoName-qu3tw
@NoName-qu3tw - 22.09.2023 23:42

Great! Thank you!

Ответить
@LorenzoJimenez
@LorenzoJimenez - 23.10.2023 05:18

You actually forgot to install typescript as a full complete explanation. Thanks for the video

Ответить
@PavanKumar-g7v1q
@PavanKumar-g7v1q - 11.11.2023 12:34

Amazing!

Ответить
@reihane2325
@reihane2325 - 12.12.2023 12:59

thanks , so helpful

Ответить
@muhammadbilalmalik7292
@muhammadbilalmalik7292 - 13.12.2023 23:28

how tsc command is automatically executing server/index.ts ?

Ответить
@phongnguyen.5322
@phongnguyen.5322 - 18.12.2023 22:40

Many help, thank you

Ответить
@naumanshigri
@naumanshigri - 29.01.2024 20:42

belot han ongs na sokor yaa

Ответить
@piotrjasiulewicz408
@piotrjasiulewicz408 - 06.03.2024 14:02

god tier level video

Ответить
@ramiworkstation
@ramiworkstation - 24.06.2024 14:03

Sweet like Sugar 🍬🍬

Ответить
@patelronak153
@patelronak153 - 28.08.2024 11:28

Great🚀🚀❤❤

Ответить
@TechAlly-07
@TechAlly-07 - 18.09.2024 10:35

thanku brother for this valuable information

Ответить
@kennedydre8074
@kennedydre8074 - 26.09.2024 14:21

Great video, I am having a problem, I can’t use ts-node with esm, I keep getting an error

Ответить
@Imam_Faheem_khan
@Imam_Faheem_khan - 04.10.2024 19:01

thanks man, step by step explan.. really help

Ответить
@statuschannel8572
@statuschannel8572 - 10.10.2024 23:04

don't use ts-node use vite-node and save your time!

Ответить
@MdSalman-w8g
@MdSalman-w8g - 18.10.2024 19:24

where can I find that wallpaper

Ответить
@Vegasguy79
@Vegasguy79 - 04.11.2024 08:30

Such a quick and clear tutorial. Wonderful job!

Ответить
@brwka
@brwka - 04.12.2024 20:11

it's not good tutorial you why you don't show how to use modules etc. ?

Ответить