How to use the listen function from http

Find comprehensive JavaScript http.listen code examples handpicked from public code repositorys.

http.listen is a method in Node.js that starts a HTTP server and listens for incoming requests on a specified port.

15
16
17
18
19
20
21
22
23
24
function start(callback) {
  if (router.isStarted === false) {
    init(function () {
      loadRoutes(function () {
        /* Lance le serveur web sur le port 3000 */
        http.listen(3000, function name(params) {
          console.log("Application is running on port 3000");
          router.isStarted = true;
          if (typeof callback != "undefined") {
            callback();
fork icon0
star icon0
watch icon1

53
54
55
56
57
58
59
60
61
62
    winston.error(err.message)
    throw err
  }
})

server.listen(port, '0.0.0.0', () => {
  winston.info('Project is now listening on port: ' + port)

  if (_.isFunction(callback)) return callback()
})
fork icon0
star icon0
watch icon1

How does http.listen work?

The http.listen method creates a new HTTP server and listens for incoming requests on the specified network address and port number, and then executes the callback function whenever a request is received. It returns the server instance.

58
59
60
61
62
63
64
65
66
67
* @param {(req: http.IncomingMessage) => string} options.loggerGetIP - use custom logic to get IP, either from headers or directly
* @param {string} options.bindingAddress - hostname for proxy to bind to
* @param {number} options.port - port for proxy to listen to
* @param {number|null} options.crossDomainPort - crossDomain port to simulate cross origin requests. set to null
* to disable using this. highly not recommended to disable this because it breaks sites that check for the origin header
* @param {boolean} options.dontListen - avoid calling http.listen() if you need to use sticky-session to load balance
* @param {http.ServerOptions} options.ssl - set to null to disable ssl
* @param {(req: http.IncomingMessage) => RammerheadServerInfo} options.getServerInfo - force hammerhead to rewrite using specified
* server info (server info includes hostname, port, and protocol). Useful for a reverse proxy setup like nginx where you
* need to rewrite the hostname/port/protocol
fork icon0
star icon0
watch icon0

177
178
179
180
181
182
183
184
185
186

#### server.listen(httpServer[, options])

Synonym of [server.attach(httpServer[, options])](#serverattachhttpserver-options).

#### server.listen(port[, options])

Synonym of [server.attach(port[, options])](#serverattachport-options).

#### server.bind(engine)
fork icon0
star icon0
watch icon2

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const http = require("http");

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader("Content-Type", "text/plain");
  res.end("Hello, World!\n");
});

server.listen(3000, () => {
  console.log("Server running on port 3000");
});

In this example, a simple HTTP server is created using http.createServer(). The server is then set to listen for incoming requests on port 3000 using server.listen().