DEV Community

Cover image for Express with Typescript starter explained (fast)
Code Oz
Code Oz

Posted on • Edited on

Express with Typescript starter explained (fast)

If you are reading this, I think you should know Typescript & Express, so let's go through this, and build something from scratch !

Before start

Let's start by checking if node is currently installed

node -v
Enter fullscreen mode Exit fullscreen mode

Install typescript (globally) if not installed

npm install -g typescript
Enter fullscreen mode Exit fullscreen mode

Initialization

So let's begin ! First of all, use npm init in order to create package.json !

So basically what are we needing ? We need to install express, install and configure typescript packages, and install @types package linked to the previous packages. We also need to install ts-node in order to build typescript file in nodejs

So let's do this

npm i express
Enter fullscreen mode Exit fullscreen mode
npm i -D typescript ts-node @types/node @types/express
Enter fullscreen mode Exit fullscreen mode

Init config typescript

tsc --init
Enter fullscreen mode Exit fullscreen mode

Create two folders:

  • src/ (will contains all ts files)

  • dist/ (will contains all builded files (js files))

Start

In order to start our project, we will use nodemon in order to watch any changes in *.ts files and re-build it automatically.

npm i -D nodemon
Enter fullscreen mode Exit fullscreen mode

Create command to execute code

  "scripts": {
    "dev": "nodemon src/app.ts"
  },
Enter fullscreen mode Exit fullscreen mode

Create app.ts file in src/ like bellow :

import express, { Application, Request, Response } from 'express'

const app: Application = express()

const port: number = 3001

app.get('/toto', (req: Request, res: Response) => {
    res.send('Hello toto')
})

app.listen(port, function () {
    console.log(`App is listening on port ${port} !`)
})
Enter fullscreen mode Exit fullscreen mode

Run server

npm run dev
Enter fullscreen mode Exit fullscreen mode

Yeah it's working ! You can now create your beautiful project in Express supporting Typescript !

Link to repo -> https://github.com/Code-Oz/basic-express-typescript


I hope you like this reading!

🎁 You can get my new book Underrated skills in javascript, make the difference for FREE if you follow me on Twitter and MP me 😁

Or get it HERE

🎁 MY NEWSLETTER

β˜•οΈ You can SUPPORT MY WORKS πŸ™

πŸƒβ€β™‚οΈ You can follow me on πŸ‘‡

πŸ•Š Twitter : https://twitter.com/code__oz

πŸ‘¨β€πŸ’» Github: https://github.com/Code-Oz

And you can mark πŸ”– this article!

Top comments (2)

Collapse
 
fredkiss3 profile image
Fredhel KISSIE

thank you it helped me a lot❀️

Collapse
 
codeoz profile image
Code Oz

happy to help you ;)