Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript:如何在NodeJS中扩展全局对象?_Typescript - Fatal编程技术网

Typescript:如何在NodeJS中扩展全局对象?

Typescript:如何在NodeJS中扩展全局对象?,typescript,Typescript,我尝试在NodeJS中扩展全局对象,比如Global.spotConfig。我一直在寻找答案,但没有合适的答案 请帮帮我,我的密码是 注意:如果服务器未运行,请在服务器控制面板上重新启动服务器请参阅 在您的情况下,您需要: var http = require("http"); declare module NodeJS { interface Global { spotConfig: string } } //create a server object:

我尝试在NodeJS中扩展全局对象,比如
Global.spotConfig
。我一直在寻找答案,但没有合适的答案

请帮帮我,我的密码是

注意:如果服务器未运行,请在服务器控制面板上重新启动服务器

请参阅

在您的情况下,您需要:

var http = require("http");

declare module NodeJS {
    interface Global {
        spotConfig: string
    }
}

//create a server object:
http
    .createServer(function (req, res) {
        // assign a new value to the global property
        global.spotConfig = "anything";
        console.log(global.spotConfig);
        // error TS2339: Property 'config' does not exist on type 'Global'.
        res.write("Hello World!"); //write a response to the client
        res.end(); //end the response
    })
    .listen(8080); //the server object listens on port 8080


你为什么要那样做?全局状态使程序变得脆弱。