freeCodeCamp.org
Уровень B2· 1182 слова
TypeScript makes Express servers safer, more scalable, and easier to maintain. In this hands-on tutorial, you'll go from setting up a TypeScript-powered Express environment to structuring routes, controllers, and middleware with full type safety. By the end of the course, you'll have the skills to build
production-ready Express APIs with confidence. >> Hello, hello, and welcome to this course on Express with TypeScript. We're going to take Express, one of the more popular back-end frameworks for Node.js, and combine it with TypeScript. So, we get the speed of building APIs with the
safety of strong typing. When I first learned Express, I loved how fast I could build something, but I also spent a lot of time guessing at types and fixing silly bugs. So, today I want to show you how to get the best of both worlds. Express's speed plus TypeScript's reliability. Over this
course, you'll build a simple read-only but fully typed pet shelter API. This API can return a list of pets, a filtered list of pets, or a single pet based on ID. Along the way, you'll learn how to set up the environment, type your apps with Express-specific concepts, work with custom types to model real
data, add more specificity to request and responses, filter results with query parameters of different types, and even add your own middleware fully typed with TypeScript. Plus, as always, lots of challenges along the way to keep you on your toes. By the end of this course, you'll not only have a working API, but
a strong mental model of how TypeScript and Express work together. Now, one quick note. This course does assume you already know the basics of Express and TypeScript. If you are new to either of those, I recommend checking out the Scrimba courses first. I've linked them both right here on the slide, so you can
hop into them, go through it, and then come back when you're ready to dive in. I'm Rachel, and I'll be your teacher for this course on Express and TypeScript. I hope you're as keen as I am to learn something new. Let's dive in.
Let's start off by installing Express and TypeScript as well as its developer dependencies. This starts off just like any other node project with npm init -y.
And now we have a very familiar file, package.json, empty and ready to go. Let's install some dependencies. We're working with Express, so of course that will be our first one. So, let's install Express.
Once Express is done installing, we'll see the node modules folder pop up, and we can also see Express listed as a dependency in package.json.
This is all we would need to get ready for Express. And now it's time to add our TypeScript. Now, TypeScript is a dev dependency, meaning it's only needed while we're developing, not after the project is built and deployed. So, we'll make sure to install the D flag.
TypeScript. Another dev dependency we'll need is a library of Express types. Express itself is written in plain JavaScript, so TypeScript doesn't automatically know about Express specific things like request or response. So, we'll also install as a dev dependency types Express. Now, this package adds the type