最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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("express");----->1
  var app = express(); --------->2 

So I understand the first statement is to load express module .What is the need for second statement??

Inorder to load a MYSQL module in my app.js we use

 var mysql = require("MYSQL");

We access SQL properties by mysql.connect("") etc.

so why cannot we write "express.get()" instead of "app.get()"????

Why do we need var express = require("express");??

Any help would be highly appreciated.

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("express");----->1
  var app = express(); --------->2 

So I understand the first statement is to load express module .What is the need for second statement??

Inorder to load a MYSQL module in my app.js we use

 var mysql = require("MYSQL");

We access SQL properties by mysql.connect("") etc.

so why cannot we write "express.get()" instead of "app.get()"????

Why do we need var express = require("express");??

Any help would be highly appreciated.

Share Improve this question edited Mar 10, 2016 at 20:37 Shadow 34.2k10 gold badges63 silver badges74 bronze badges asked Mar 10, 2016 at 20:10 RisTeRisTe 1231 silver badge4 bronze badges 3
  • 1 Because that is a design decision of the corresponding library. In mysql you have multiple main factory functions createPool, createPoolCluster and createConnection, for express only one, this is most likely the reason for this difference. But in the end its up to the library creator how to design the api. – t.niese Commented Mar 10, 2016 at 20:18
  • @t.niese Hey,Thanks for the quick reply.However,I am having a little bit of difficulty in understanding your statement.Can you please illustrate through an example? If possible? :) :) – RisTe Commented Mar 10, 2016 at 20:21
  • app.get(...) is equal to connection.query(...) and express() is equal to mysql.createConnection(...). mysql is in this case just a namespace grouping the factory functions of the mysql library, express returns the main factory function directly. They also could have decided that you need to do var app = express.createApp() but they didn't. – t.niese Commented Mar 10, 2016 at 20:34
Add a comment  | 

2 Answers 2

Reset to default 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')();

The real difference between require('express') and express() is that require('express') allows you to have access to any public functions or properties exposed by module.exports.

The express() syntax is the equivalent of saying new express(). It creates a new instance of express that you can then assign to a variable and interact with.

That is why the standard creation pattern for Express is

// Import the Express module
var express = require('express');

// Create a new Express Instance
var app = express();
发布评论

评论列表(0)

  1. 暂无评论