Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 什么是公共应用程序和express.Application_Javascript_Typescript - Fatal编程技术网

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

Javascript 什么是公共应用程序和express.Application,javascript,typescript,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();

我在用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);
});

我无法理解他们在这里做什么以及为什么。有人能帮我理解一下吗?

首先,
public
不是typescript关键字,而是js类语法


Typescript语法以
开头,之后是一个类型,可以是以下几种类型之一:Typescript接口、Typescript数据类型、对象;在本例中,在名为
Application
的属性中,在名为
express

的导入中,我建议阅读一篇基本的TypeScript教程;这是一个类型化的属性声明。从例如开始;你的具体问题将在课堂部分得到回答。类似的一行是:
publicsomeword:String=“example”感谢您的回答,您能解释一下为什么路由是私有的而配置是公共的吗?您可以在这里访问应用:
const-app=new-app().app因此它必须是公共的<代码>路由
您不能从外部访问,因此最好将它们设置为私有。顺便说一句,默认情况下,所有类属性都是公共的-写出public关键字只是显式的,但不是必需的。