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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38941636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:44:51  来源:igfitidea点击:

How to use Node `http` module in TypeScript

javascriptnode.jstypescript

提问by Andry

I need to write a server in TypeScript and Node.

我需要用 TypeScript 和 Node.js 编写一个服务器。

  1. I downloaded Nodefrom DefinitelyTypedrepository
  2. I created my typescript file
  3. Imported the definition
  4. Tries to use it
  1. 我从绝对类型的存储库下载了节点
  2. 我创建了我的打字稿文件
  3. 导入定义
  4. 尝试使用它

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 httpmodule. Where can I find the types? In the definition file i am having hard time recognizing this information.

但我无法理解如何引用该http模块。我在哪里可以找到这些类型?在定义文件中,我很难识别这些信息。

回答by Jorawar Singh

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

那是因为你正在使用require. 使用import而是将承认并也会给你不错的智能感知:-)

import * as http from "http"