简体   繁体   中英

How to use Node `http` module in TypeScript

I need to write a server in TypeScript and Node.

  1. I downloaded Node from DefinitelyTyped repository
  2. I created my typescript file
  3. Imported the definition
  4. Tries to use it

The result is:

/// <reference path="definitions/commonjs.d.ts" />
/// <reference path="definitions/node.d.ts" />

var http = require("http");

namespace MyProj {
    export class Server {
        public run() {
            var server = http.createServer(); // TypeScript does not recognize 'http'
        }
    }
}

But I cannot understand how I can reference the http module. Where can I find the types? In the definition file i am having hard time recognizing this information.

It's because of you are using require . use import instead it will recognize and also will give you nice intellisense :-)

import * as http from "http"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM