简体   繁体   English

如何在TypeScript中使用Node`http`模块

[英]How to use Node `http` module in TypeScript

I need to write a server in TypeScript and Node. 我需要在TypeScript和Node中编写服务器。

  1. I downloaded Node from DefinitelyTyped repository 我从DefinitelyTyped存储库下载了Node
  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. 但我无法理解我如何引用http模块。 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 . 这是因为你正在使用require use import instead it will recognize and also will give you nice intellisense :-) 使用import而不是它会识别并且还会给你很好的intellisense :-)

import * as http from "http"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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