什么是公共应用程序和Express.Application

时间:2019-07-10 10:12:29

标签: javascript typescript

我正在用Node学习TypeScript,有人在其中写了这样的一行

  public app: express.Application;

在以下情况下

import express, { Request, Response }  from "express";
import bodyParser from "body-parser";

class App {

  constructor() {
    this.app = express();
    this.config();
    this.routes();
  }

  //TODO: What is public app: express.Application
  public app: express.Application;

  private config(): void {
    this.app.use(bodyParser.json());
    this.app.use(bodyParser.urlencoded({ extended: false }));
  }

  private routes(): void {
    const router = express.Router();
    router.get('/', (req: Request, res: Response) => {
      res.status(200).send({
        message: 'Hello World!'
      })
    });
    router.post('/', (req: Request, res: Response) => {
      const data = req.body;
      // query a database and save data
      res.status(200).send(data);
    });
    this.app.use('/', router)
  }
}

const app = new App().app;
const port = 4040;

app.listen(port, function() {
  console.log('Express server listening on port ' + port);
});

我无法理解他们在这里做什么以及为什么这么做。有人可以帮助我理解它吗?

1 个答案:

答案 0 :(得分:1)

首先,public不是打字稿关键字,而是js类语法。

Typescript语法以:开头,之后是一个类型-可以是以下类型之一:Typescript接口,Typescript数据类型,对象;在这种情况下,导入的Application属性为express

相关问题