Skip to content

prentece/http_server_typescript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 

Repository files navigation

HTTP Server with Typescript

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.

Starting point

Prerequisites

  • Install NodeJS version 20 or later [ fast ] [ better ]

From scratch

  • 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.

Next step

  • 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"

Fastify

  • 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

Express

  • 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published