TypeScript and NodeJS: The Proper Setup!
HTML-код
- Опубликовано: 3 дек 2022
- 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 - Наука
Your explanation is very clear
Enjoyed watching your video!
Many help, thank you
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.
Hi greggsworld. You should be able to do that with something like this:
"dev": "tsc-watch --onSuccess \"yarn start\"",
"start": "node dist/app.js"
using tsc-watch will keep an eye out for code changes
Amazing!
thanks , so helpful
Great! Thank you!
You actually forgot to install typescript as a full complete explanation. Thanks for the video
god tier level video
Hey, can you make a video explaining hot set up next-auth with a local database?
Why dir called "server" and no "src"? (naming best practice)
I was going to ask this. Still, great video.
how does tsc still work after you moved it into a folder? That doesnt make sense.
gooodddd, thank youuuu
how tsc command is automatically executing server/index.ts ?
do you have a link to your github or is it private
subscribed
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" ?
Probably the biggest in the industry. What else compares in adoption?
Low level systems use a lot of C but companies want many good devs and good libs. So community size is a proxy for that. Js has biggest community, node is server js.
yeah LinkedIn and nasa are just hobby backend thing
@@0xSLN javascript is so big because it is basically required on the front-end. On the back-end, there are tons of options that are faster and more mature than node.js. I doubt it's the most popular.
@@redeemr Its based on number of projects on github. But popular doesn't equal best, lots of langs that are better at specific things.
Express has around 22M+ weekly downloads as of today
Sweet like Sugar 🍬🍬
8:20 Wait what? That wouldn't work on Browsers, you can't import a Node Library like the fs one, in a browser, right?
node doesn't run in browsers.
had the same doubt, yeah ur correct, brainfade from the guy.
belot han ongs na sokor yaa
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;
you're mixing require and esm syntax. You should look into what adding "type": "module" does or what using the .cjs and .mjs file extensions do. I'm sure you've already solved the problem but I'm just leaving this here for future reference.