Guide to Setting Up an HTTP Server Using TypeScript. Instructions for configuring and running a simple server, ensuring you can quickly start developing your applications.
-
Run in terminal
npm init -y npm install typescript @types/node tsx -D npx tsc --init
-
Update tsconfig.json using the microsoft recommendations Node-Target-Mapping.
-
Basic Structure
mkdir -p src/http touch src/http/server.ts
-
Update the "scripts" property for package.json to run tsc in watch mode.
"dev": "tsx watch src/http/server.ts"
-
-
Start here Getting Started.
npm install fastify
-
Update the server.ts file with the code below.
import fastify, { FastifyInstance } from "fastify"; const app: FastifyInstance = fastify(); const port = 3333; app.get("/", () => { return { hello: "world" }; }); app.listen({ port }).then(() => { console.log(`HTTP server running on http://localhost:${port}`); });
-
Run your server
npm run dev
-
-
Start here Getting Started.
npm install express npm install @types/express -D
-
Update the server.ts file with the code below.
import express, { Request, Response } from "express"; const app = express(); const port = 3333; app.use(express.json()); app.get("/", (req: Request, res: Response) => { res.json({ message: "Hello, world!" }); }); app.listen(port, () => { console.log(`HTTP server running on http://localhost:${port}`); });
-
Run your server
npm run dev
-