Lesson 34 of 56 · Typescript
Node.js Basics with TypeScript
Duration: 20 minutes
Initialize a Node TS project
mkdir ts‑node && cd $_
npm init -y
npm i -D typescript @types/node
npx tsc --init
Modify tsconfig.json to target CommonJS.
"module": "CommonJS",
"outDir": "dist",
Simple HTTP server with http
import http from 'http';
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from TypeScript Node!');
});
server.listen(3000, () => console.log('🟢 listening on 3000'));
Run with npx ts-node src/server.ts or compile with tsc then node dist/server.js.
Using process.env with typings
const PORT = Number(process.env.PORT ?? 3000);