如何从外部 link 导入 Typescript 或 Javascript 文件?
How to import a Typescript or Javascript file from an external link?
我想使用 Node.js 中的一些 Deno 标准库来创建一个 HTTP 服务器。
我知道我可以下载它,但我想保持最新的库更新,所以我想像这样导入它们:
import { serve } from "https://deno.land/std/http/server.ts";
// Defining port to be used
const PORT = 8080
// Setting server to listen at port
const server = serve({ port: PORT });
console.log(`This Hello World server is up and running on http://localhost:${PORT}/`);
// Sending Hello World to any client that connects
for await (const req of server) {
req.respond({ body: "Hello World!\n" });
}
理论上可以在 Node 中使用 Deno std
库的有限子集(模块不使用任何来自网络 API 的模块,不使用任何来自 Deno API 的模块,并且不包括带有以 .ts
).
结尾的说明符的导入语句
但是,您在 https://deno.land/std/http/server.ts
的示例中的模块不符合此条件,并且会在 tsc
(TypeScript) 中产生编译时错误,在 Node.js 中产生运行时错误。
没关系,我只是想使用 ES6+ 语法创建一个服务器,所以我使用这个:
import https from "https";
import http from "http";
export default class Server {
readonly server: http.Server | https.Server;
constructor(options?: http.ServerOptions | https.ServerOptions, httpsMode?: boolean) {
this.server = (httpsMode ? https : http).createServer(options);
}
listen(port?: number, hostname?: string, backlog?: number) {
let s = this.server;
s.listen(port, hostname, backlog);
return {
[Symbol.asyncIterator]() {
return {
async next() {
return {
done: false,
value: await new Promise<{request: http.IncomingMessage, response: http.ServerResponse}>(result => {
s.on('request', (request, response) =>
result({request, response})
);
})
};
}
}
}
}
}
close() {
this.server.close();
}
}
在我发布 0.0.4
之后,这个模块在 NPM 上可用,包 Newer.js
我想使用 Node.js 中的一些 Deno 标准库来创建一个 HTTP 服务器。
我知道我可以下载它,但我想保持最新的库更新,所以我想像这样导入它们:
import { serve } from "https://deno.land/std/http/server.ts";
// Defining port to be used
const PORT = 8080
// Setting server to listen at port
const server = serve({ port: PORT });
console.log(`This Hello World server is up and running on http://localhost:${PORT}/`);
// Sending Hello World to any client that connects
for await (const req of server) {
req.respond({ body: "Hello World!\n" });
}
理论上可以在 Node 中使用 Deno std
库的有限子集(模块不使用任何来自网络 API 的模块,不使用任何来自 Deno API 的模块,并且不包括带有以 .ts
).
但是,您在 https://deno.land/std/http/server.ts
的示例中的模块不符合此条件,并且会在 tsc
(TypeScript) 中产生编译时错误,在 Node.js 中产生运行时错误。
没关系,我只是想使用 ES6+ 语法创建一个服务器,所以我使用这个:
import https from "https";
import http from "http";
export default class Server {
readonly server: http.Server | https.Server;
constructor(options?: http.ServerOptions | https.ServerOptions, httpsMode?: boolean) {
this.server = (httpsMode ? https : http).createServer(options);
}
listen(port?: number, hostname?: string, backlog?: number) {
let s = this.server;
s.listen(port, hostname, backlog);
return {
[Symbol.asyncIterator]() {
return {
async next() {
return {
done: false,
value: await new Promise<{request: http.IncomingMessage, response: http.ServerResponse}>(result => {
s.on('request', (request, response) =>
result({request, response})
);
})
};
}
}
}
}
}
close() {
this.server.close();
}
}
在我发布 0.0.4
之后,这个模块在 NPM 上可用,包 Newer.js