0

I am trying to create an instance of a javascript class but i keep getting the error:

TypeError: Server is not a function

Not sure how to get this to work correctly. My class looks like this:

server.js

class Server {
    #app;
    #port;
    constructor(port) {
        this.#port = port;
        this.#app = require('express')();
    }
    setStaticDirectory(directory) {
        this.app.use(express.static(directory));
    }

    init() {
        console.log(this.#app.listenerCount);
        this.#app.listen(port);
    }
}

And then in my main file i have:

main.js

var Server = require("./server.js");
const PORT = 3000;
const STATICS = 'public';
const SERVER = new Server(PORT); // error is here

SERVER.setStaticDirectory(STATICS);
SERVER.init();

What am i missing here? I come from a C# background so i am a bit confused why i get this error.

WDUK
  • 1,412
  • 1
  • 12
  • 29
  • 1
    How are you exporting `Server` from `server.js`? Also, what version of Node.js are you running? – Phil Jul 26 '23 at 23:01
  • Not sure what you mean by exporting, i am still new to this. Most tutorials never explained how to link things between different files. I assumed require gives me access to create an instance of Server? – WDUK Jul 26 '23 at 23:02
  • 1
    Read https://nodejs.org/api/modules.html (and https://nodejs.org/api/esm.html for the modern way) – Bergi Jul 26 '23 at 23:03
  • It would / should be something like `module.exports = Server` – Phil Jul 26 '23 at 23:04
  • _"Most tutorials never explained how to link things between different files"_... which tutorials are you following? I'd be happy to take a look and point you in the right direction – Phil Jul 26 '23 at 23:07

0 Answers0