Creating Nodejs Express Project with Typescript
First, let’s talk a little about why we need to use Typescript.
When JavaScript was originally designed, it was not designed for large-scale application development. That is, in Javascript, C#, as in Java, classes, interfaces, modules, etc. structures are not available. This makes it difficult to develop large projects with JavaScript t.
Then TypeScript comes to our rescue. TypeScript frees us from the frustrations of JavaScript and enables us to develop large-scale applications. While TypeScript provides all the possibilities that JavaScript provides, it allows us to use structures such as enum, interface (and more) on it. Therefore, we can say that TypeScript is a superset of JavaScript.
How TypeScript Works
When TypeScript code is compiled, JavaScript code is actually produced as output. In other words, structures such as enum and interface that we use in Typescript are translated into valid Javascript code. Some structures (for example, interfaces) that we use to organize our code are not even translated into JavaScript. They are only used by Typescript during the compilation process. So we get a clean Javascript output. In addition, since Typescript only takes charge during compilation, there is no performance problem that may arise from TypeScript at runtime. Because when Typescript code is compiled and Javascript code is produced, Typescript’s job ends here. In addition, since the code that comes out is purely JavaScript, you do not need to use any extra runtime or library, etc., since you are using TypeScript.
So let’s start..
First, let’s create a folder for our project. Let’s go into the folder where we will create the folder by opening the terminal and create the folder of our project.
cd desktopmkdir typescript-node
Let’s go inside our project folder and create a “package.json” file.
npm init
After typing this command, you will have a file like the one below.
Now, let’s install the relevant packages via “npm” and continue. Execute the following commands in order.
npm i @types/express @types/node nodemon ts-node typescript -Dnpm i-express
After executing the commands, the package.json file will be as follows.
Now, let’s add the following commands between the “script” tag.
“start”: “node dist/app.js”,
“dev”: “nodemon app.ts”,
“build”: “tsc -w”
start, dev, and build commands, it’s time to create the tsconfig.json file. Let’s create the file with the command below and add its content.
nano tsconfig.json
Let’s add the following content to the page that opens.
After all the settings were completed, it was time to raise our express server. Let’s create a file named “ app.ts ” in our project directory and launch express.
Let’s run and test :) Follow the commands below in order.
npm install
npm run build
npm start
If everything went well, you will see a result like the one below.
Application is listen 3000
You can see the result by going to “localhost:3000” in your browser . The result should be like below.
{success:true, message:”Hello world”}