我想用 typescript 制作node.js服务器
///<reference path="definitions/node.d.ts" />
///<reference path="core/p_routing.ts" />
class server{
private _port = 1337;
private _host = '127.0.0.1';
constructor(){
require("node-ts");
var http = require('http');
http.createServer(function (req, res) {
new pMVC.p_routing(req, res);
}).listen(this._port, this._host);
console.log('Server running at http://127.0.0.1:1337/');
}
}
var serverw = new server();
这是我的 server.js
module pMVC {
export class p_routing {
private _url:string[];
private _defaultController:string = 'home';
private _defaultMethod:string = 'index';
private _fs = null;
public constructor(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}
}
}
这是我的路由类。 (将是路由类)
我的编辑器中没有任何错误。但是当我尝试访问网站时,服务器死机并且出现以下错误:
最佳答案
在没有任何根级别导入
/导出
的情况下, typescript 编译器假设所有内容都在全局名称范围内。
在 Node.js 运行时中情况并非如此,您应该使用带有 import
/require
关键字的 commonsjs
模块。更多:https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1
PS:即使对于非 Nodejs 项目,我也建议使用外部模块:https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md
关于javascript - 用 typescript 编写 Node.js 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30500733/