TypeScript and Express
I assume you've already installed Nodejs. If not, you can install it for your Operating System from its official site.
Now let's begin!
Creating the Project
First of all, let's create a project directory. Let's name it express (or whatever else you prefer).
Open a terminal in that directory and initiate a node project by typing the command:
It will ask a bunch of fields to insert for the basic information about your project. Insert what you like, or let it be.
Now, let's install typescript and express using the command :
Let's initialize the typescript config for the project using the command :
It will create a node_modules folder in the project directory, along with package.json, package-lock.json and tsconfig.json files. You can configure typescript settings by modifying the tsconfig.json file.
For now, you can use my config to keep the project running.
Now let's create a directory called src and our main file index.ts inside src. You can do it in your code editor, using file manager, or by running the command :
So far, we have created our project with working directory src. Now let's code.
Edit your index.ts file to be:
With the code above, we are using Express to create the default and jr routes. The default routes send a text message, whereas the jr route sends a json.
Let's compile the typescript according to our typescript config file tsconfig.json:
Now, we can run the project using:
Hit http://127.0.0.1:3000 and http://127.0.0.1:3000/jr in your browser and check the result. The default route will show the text "Welcome to Texpress" in your browser, and the jr route will send you the json given in the code.
This is just a basic how-to with typescript and express. Hope it helped!