将app作为http.createServer的参数

时间:2016-09-23 08:53:43

标签: javascript node.js express typescript typescript-typings

当我想使用express.Application作为http.createServer的参数时,我有这个错误:

error TS2345: Argument of type 'Application' is not assignable to parameter of type '(request: IncomingMessage, response: ServerResponse) => void'.

我的代码:

import * as express from "express"
let app: express.Application = express();
...
import * as http from "http"
let httpServer = http.createServer(app);
httpServer.listen(process.env.HTTP_PORT, (): void => {
    console.log(`HTTP Listen on ${process.env.HTTP_PORT}`)
});

typings.json:

{
  "dependencies": {
    "body-parser": "registry:npm/body-parser#1.15.2+20160815132839",
    "express": "registry:npm/express#4.14.0+20160911114220"
  },
  "globalDependencies": {
    "dotenv": "registry:dt/dotenv#2.0.0+20160327131627",
    "errorhandler": "registry:dt/errorhandler#0.0.0+20160316155526",
    "express-serve-static-core": "registry:dt/express-serve-static-core#4.0.0+20160914120416",
    "method-override": "registry:dt/method-override#0.0.0+20160317120654",
    "morgan": "registry:dt/morgan#1.7.0+20160524142355",
    "node": "registry:dt/node#6.0.0+20160921192128"
  }
}

1 个答案:

答案 0 :(得分:0)

我解决了。 只需要更新打字。我目前的typings.json文件:

{
  "globalDependencies": {
    "dotenv": "registry:dt/dotenv#2.0.0+20160327131627",
    "errorhandler": "registry:dt/errorhandler#0.0.0+20160316155526",
    "express": "registry:dt/express#4.0.0+20160708185218",
    "express-serve-static-core": "registry:dt/express-serve-static-core#4.0.0+20160916114806",
    "method-override": "registry:dt/method-override#0.0.0+20160317120654",
    "morgan": "registry:dt/morgan#1.7.0+20160524142355",
    "node": "registry:dt/node#6.0.0+20160921192128",
    "serve-favicon": "registry:dt/serve-favicon#0.0.0+20160316155526"
  },
  "dependencies": {
    "body-parser": "registry:npm/body-parser#1.15.2+20160815132839",
    "serve-static": "registry:npm/serve-static#1.11.1+20160810053450"
  }
}