Building an Express Server Using TypeScript
Create a folder called server
type cd server or open the folder in vs code
Execute This Command
npm init -y
Install Typescript
npm i typescript npm install -g typescript npm i -D typescript @types/node
Install libraries With Thier Types
npm i express @types/express
npx tsc init
create src and dist folder into server folder
open tsconfig.json
change rootDir “./” to “./src”
change outDir “./” to “./dist”Create Folders Like Middleware, Models, Route And Controller Into Src Folder
Change package.json File Like Below Mentioned:
{ "name": "server", "version": "1.0.0", "main": "server.js", "scripts": { "dev":"tsc -b && node ./dist/server.js" }, "keywords": [], "author": "", "license": "ISC", "description": "", "dependencies": { "typescript": "^5.7.2" } }
run npm run dev in the terminal
npm i @types/express @types/bcrypt @types/cookie-parser @types/cors
Conclusion
By This U can create your express server in typescript, In we have learn how to setup typescript in our express project or globally and learned to install types of packages used in building express server.