用打字机写node.js服务器
我想用打字机制作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'); } } }
这是我的路由类。 (将会是路由类)
编辑器中没有任何错误 但是当我试图访问网站,服务器死亡,我有以下错误:
在没有任何根级import
/ export
的情况下,打字稿编译器将假定所有内容都位于全局名称范围内。
这在Node.js运行时中不适用,您应该使用import
/ require
关键字来使用commonsjs
模块。 更多: https : //www.youtube.com/watch?v=KDrWLMUY0R0&hd=1
PS:我build议使用外部模块,即使是非nodejs项目: https : //github.com/TypeStrong/atom-typescript/blob/master/docs/out.md
- DefinitelyTyped如何将声明文件绑定到JavaScript库?
- 在nodejs中inputTypeScript模块
- 从几个TypeScript类创build一个CommonJS模块
- Typescript / NodeJS – 与工厂合作时的循环依赖
- 添加服务器端代码到angular-cli项目与节点types冲突
- 在使用global.Promisereplacemongoose的mpromise库时出现TypeScript错误
- 如何使用ThreeJS Collada loader与TypeScript / Angular CLI?
- Async / Await – Typescript和ExpressJs
- (node.js模块)清晰的image processing器保持源文件打开,resize后无法解开原始链接