What is the use of var app = express(); in the process of creating a Node.Js Application?

后端 未结 2 1809
梦毁少年i
梦毁少年i 2021-02-04 14:46

I am new to the world of Node.js.I am trying to learn through an example.I have encountered the following statements in the \"app.js\" .

  var express = require(         


        
2条回答
  •  隐瞒了意图╮
    2021-02-04 15:15

    express is a module that can be used to create more than one application.

    var ex = require('express')
    

    puts this module into the variable ex. Once you have a reference to the module, you can use it to create application. Each module has its own API. According to the expressjs documentation - http://expressjs.com/en/4x/api.html, the module is in fact a function that can be used to create applications

    var app1 = ex();
    var app2 = ex();
    

    you could for example want to have several web applications listening on different ports.

    If you only want one application (but it would be less readable) you could write

    var app = require('express')();
    

提交回复
热议问题