Typescript:如何在NodeJS中扩展全局对象?
我尝试在NodeJS中扩展全局对象,比如Typescript:如何在NodeJS中扩展全局对象?,typescript,Typescript,我尝试在NodeJS中扩展全局对象,比如Global.spotConfig。我一直在寻找答案,但没有合适的答案 请帮帮我,我的密码是 注意:如果服务器未运行,请在服务器控制面板上重新启动服务器请参阅 在您的情况下,您需要: var http = require("http"); declare module NodeJS { interface Global { spotConfig: string } } //create a server object:
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
你为什么要那样做?全局状态使程序变得脆弱。