【问题标题】:How to type app.get('/events', (req, res) in express with TypeScript如何使用 TypeScript 在 express 中键入 app.get('/events', (req, res)
【发布时间】:2019-08-22 11:41:28
【问题描述】:

我想知道在express中使用app.get时(req, res) =>的类型注释是什么。

app.get('/events', (req, res) => {
        // SSE Setup
        res.writeHead(200, {
            'Content-Type': 'text/event-stream',
            'Cache-Control': 'no-cache',
            'Connection': 'keep-alive',
        });
        res.write('\n');

        sseEvents(req, res);
    });

【问题讨论】:

    标签: node.js typescript express


    【解决方案1】:

    您需要拥有@types/express (npm i -D @types/express) 然后才能使用

    import { Request, Response } from 'express';
    app.get('/events', (req: Request, res: Response) => {
      // your code
    });
    

    【讨论】:

      猜你喜欢
      • 2022-12-11
      • 2017-07-22
      • 2021-03-07
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多