Header Ads

Header ADS

Server Creation

শুধু Node.js + TypeScript দিয়ে একটি সার্ভার


🔹 Step 1: প্রজেক্ট সেটআপ

npm init -y

🔹 Step 2: TypeScript + ts-node-dev ইনস্টল

npm install  typescript  @types/node nodemon

🔹 Step 3: TypeScript কনফিগারেশন

npx tsc --init

tsconfig.json এ এভাবে আপডেট করো:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "rootDir": "src",
    "outDir": "dist",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  }
}

🔹 Step 4: ফোল্ডার স্ট্রাকচার

ts-node-server
 ┣ src
 ┃ ┗ index.ts
 ┣ package.json
 ┣ tsconfig.json

🔹 Step 5: Node.js HTTP সার্ভার (src/index.ts)

import  {createServer,  IncomingMessage, ServerResponse , Server } from "http";

const PORT = 5000;

const myServer:Server = createServer((req: IncomingMessage, res: ServerResponse) => {

    res.writeHead(200, { "Content-Type": "text/plain" });
    res.end("🚀 Hello from Node.js + TypeScript server (no Express)!");
    });
    myServer.listen(PORT, () => {
      console.log(`✅ Server running at http://localhost:${PORT}`);
    });

🔹 Step 6: package.json স্ক্রিপ্ট

"scripts": {
  "build": "tsc --watch",
  "start": "nodemon dist/index.js"
}

🔹 Step 7: সার্ভার চালানো

npm run start

👉 ব্রাউজারে http://localhost:5000 খুললে দেখবে:

🚀 Hello from Node.js + TypeScript server (no Express)!



🔹 All Datatypes

Server Object ---> Server
Request ---> IncomingMessage 
Response ---> ServerResponse

আমরা যখন @types/node এই module টা install  করেছি তখন http module এর মধ্যে এই datatype গুলো চলে এসেছে । আমরা যদি @types/node module টি install না করতাম তাহলে এই  datatype গুলো ব্যবহার করতে পারতাম না । 
Powered by Blogger.