打字稿类中的公共和私人

时间:2019-07-10 13:06:02

标签: javascript typescript

我正试图在Javascript / typescript类中理解公共和私有

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);
});

此代码来自following blog

在这里,我想了解为什么,路由是私有的,而config等是公开的?

0 个答案:

没有答案