What is the function of the.Switch app in Express?

Asked

Viewed 2,316 times

1

I recently started my studies in Nodejs and Express. From what I’ve been reading, app.Leann is basically what makes the server listen to requests coming from the defined port.

But I could notice that when running an Express application it runs normally even though there is no door listening statement. In case the express runs it under the table or something? Maybe I’m asking silly, but I couldn’t find anything on the Internet to clear my doubts

This one is express-generated scaffolding. As the code shows, it has no Systener, but it works and processes incoming requests normally.

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

/// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


module.exports = app;
  • 1

    What do you mean by "run normally"? The function of an Express application is to meet HTTP requests. If the app isn’t listening to any doors, it can’t do that.

  • I edited there and added the code. Take a look

  • 1

    When it has no Listen the application runs on the standard Node door which is 3000.

  • So when not set, Node already has a default, right?

  • Take a look at your package.json. Maybe you have an npm script running the server. To be honest, I don’t know much about express.

1 answer

2


The function app.listen() Express starts a UNIX socket and listens to the connections in a given path.

The function app.listen(port, [hostname], [backlog], [callback]) Express is the same http.server.listen([port][, hostname][, backlog][, callback]) node. According to the Node documentation, if no port is passed as parameter, a random port will be used.

Omit the port argument, or use a port value of 0, to have the Operating system assign a Random port, which can be retrieved by using server.address().port after the 'Listening' Event has been Emitted.

const express = require('express')()

express.listen(undefined, 'localhost');

express.get('/', function (req, res) {
  res.send('Hello World!')
})

It will start a server without a specific port. Because of Node (and not Express), a random port will be defined.

That is, it is possible to start Express without specifying a port, but it is not possible to start Express without saying where to listen.

In my tests, every time I tried to start the Express without the app.listen he leaves immediately.

In this case, you can check if the application is not running in background already.

To check which port the Node is listening to:

lsof -i | grep node

Will return something like this:

node ... TCP localhost:57466 (LISTEN)

  • But his question was whether the express starts the server even without the call to app.listen(). Or did I misunderstand?

  • I edited it to try to clarify a little.

  • Ahh yeah. I got it, man. Thank you so much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.