What is function createServer(requestListener?: RequestListener):server?

I am new to NodeJS.
I am not very familiar with this statement:function createServer(requestListener?: RequestListener):server. Why does this method have :server, and why are the parameters inside not separated by commas, but uses ?:?

Any ideas on how to fix this problem

26 thoughts on “What is function createServer(requestListener?: RequestListener):server?”

  1. This is either TypeScript, or type notation included in documentation. It is not valid JavaScript code.

    function createServer(requestListener?: RequestListener): Server;

    means that createServer can accept a parameter – if a parameter is passed, the parameter must of type RequestListener (which is a callback). The question mark there indicates that the parameter is optional; createServer can also be called with no parameters at all.

    The colon after the parameter list indicates the return type of the function. If you call createServer, you will get something of type Server in return.


Leave a Comment