TypeScript and NodeJS: The Proper Setup!

preview_player
Показать описание
Let's set up TypeScript with NodeJS properly! To do that, we'll take a look at what precompiling is, to then look at how TypeScript gets compiled to Javascript in action. Really interesting stuff. And after understanding that, we can take a look at an npm-package that does that stuff under the hood for us.

Thanks for watching, I hope you found this as interesting as I did! Cheers
Рекомендации по теме
Комментарии
Автор

thanku brother for this valuable information

blackrolex-gibg
Автор

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

LorenzoJimenez
Автор

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.

greggsworld
Автор

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

ballonura
Автор

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

ilearncode
Автор

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

muhammadbilalmalik
Автор

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" ?

ponchobob
Автор

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

okage_
Автор

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


export default errorHandler;


SyntaxError: Unexpected token 'export'

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

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

app.use(express.json());
app.use("/api/contacts",
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;

WarframeCrunch