简体   繁体   English

Typescript 和 Express 中的错误处理 Node.js

[英]Error handling with Typescript and Express in Node.js

I'm starting with Node and Typescript and I have a question.我从 Node 和 Typescript 开始,我有一个问题。

I have these 2 files.我有这两个文件。

server.ts服务器.ts

import express = require('express');
import IConfiguration from "../config/config";


export default class Server {

    public app: express.Application;
    public configuration: IConfiguration;

    constructor( configuration: IConfiguration ) {
        this.configuration = configuration;
        this.app = express();
    }

    static init(configuration: IConfiguration) {
        return new Server(configuration);
    }

    start(callback: Function) {
        this.app.listen(this.configuration.port, callback());
        this.app.use(express.static(this.configuration.public_path))
    }
}

index.ts索引.ts

import Server from './server/server';
import Configuration from "./config/config";
import * as dotenv from 'dotenv';
import router from './router/router';

//Loading .env file
dotenv.config();

//Init config
const configuration = Configuration.init();

//Init server
const server = Server.init(configuration);
server.app.use(router);
server.start(() => {
   console.log('Server running at port ' + configuration.port);
});

In index.ts I have the app.listen callback on start function, but I dont know how write this error handling for capture the errors.在 index.ts 中,我在启动 function 时有 app.listen 回调,但我不知道如何编写此错误处理以捕获错误。 How I can add this.on("error") on my callback function?如何在我的回调 function 上添加 this.on("error")?

server.start(() => {
   console.log('Listening on port: ', port);
}).on('error', (e) => {
   console.log('Error happened: ', e.message)
});

I have tried put this in my index.ts but I get this error: "Property 'on' does not exist on type 'void'"我试过把它放在我的 index.ts 中,但我收到了这个错误:“属性 'on' 不存在于类型 'void'”

Your server variable is an instance of your Server class and when you call start it does not return anything, it just implicitly returns undefined (or void in Typescript terms) and undefined surely does not have on method, so you cannot chain it like that.您的server变量是您的Server class 的一个实例,当您调用start它不返回任何内容时,它只是隐式返回undefined (或 Typescript 术语中的void )并且undefined肯定没有on方法,因此您不能像这样链接它。 This is what error message says, basically.基本上,这就是错误消息所说的。

What you can do is return this.app from start method, for example.例如,您可以做的是从start方法返回this.app

    start(callback: Function) {
        this.app.listen(this.configuration.port, callback());
        this.app.use(express.static(this.configuration.public_path))
        return this.app;
    }

Or separate this two line like this:或者像这样分开这两行:

server.start(() => {
   console.log('Listening on port: ', port);
});

server.app.on('error', (e) => {
   console.log('Error happened: ', e.message)
});

you cannot pass "err" to app.listen in express.你不能将“err”传递给 app.listen。 I think you guys are confusing with http server:我认为你们对 http 服务器感到困惑:

const http = require('http');常量 http = 要求('http'); const server = http.createServer(app);常量服务器 = http.createServer(app);

server.on('error', (err) => {
  if (err) {
    console.log(err);
  }
});
 
server.listen(PORT, () => {
  console.log('Running')
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM