Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
Is there a way to return express instances in TypeScript classes?

I'm trying to create the express server for my project in an external module. Everything works fine until I try to do something with the app object in my main file.

server.ts:

import express from "express";
import OutputLevel from "../types/OutputLevels";

class Server {
    /**
     * Creates an express web server
     * @param {Number} port The port the server is listening on
     * @param {OutputLevel} [outputLevel] The level of console output (commonly based on CLI arguments) {@link OutputLevel}
     * @returns {Express}
     */
    constructor(port: Number, outputLevel?: OutputLevel) {
        const app = express();
        app.listen(port, () => {
            if (outputLevel && outputLevel >= 2) console.log(`[Server @ ${port}] Listening @ http://localhost:${port}`);
        });
        return app;
    }
}

export default Server;

index.ts:

import Server from "./server/server";
import OutputLevel from "./types/OutputLevels";

let app = new Server(2020, OutputLevel.INFO);

app.get("/", (req, res) => {
    res.send("hello world")
});

The get part gets marked as an error by intellisense with the note Property 'get' does not exist on type 'Server'.. Maybe this has something to do with me using classes and constructors instead of functions, but I'm trying to use a class here.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda