It is true that nodejs is developing too fast. There are two reasons for this: I bought this book "Amazing node" last winter. JS, which introduces the Express version or 2. x. x; a friend bought the node. JS practice, the result depends on express 3. x. x. If Nima installs the new version of express4, the examples in these books cannot even run properly.
Nodejs-related paper copies are out of date as soon as they are listed!
The background of this article is due to a shortage of funds one day. After upgrading the latest express V4 +, I found that the project was unavailable (Fortunately, it was a small tool within the company). After searching for the information, I sorted out some summary, describes how to use express4 to build a project.
This is almost the case, so let's "get down to it" with an express project.
If you want to know how to install and use express 4, you can view: http://www.cnblogs.com/Darren_code/p/node_express.html
--------------------------- Split line ---------------------------
Directory
- Establish Engineering Environment
- NPM Installation Details
- Significance of global mode installation
- Create a project
- Directory structure
- Configuration File magic package. JSON
Establish Engineering Environment
Let's first introduce various environments:
Go to the project directory:
cd /home/work/node/project
Run the-G command to enable global installation for NPM:
$ npm install -g express
If the version is earlier than express 4.0, Run "express-V" to get the version number. After express 4.0, you need to install the express-generator package again, if the "express-V" command is not executed, an error is returned.
Run the following command to install the express-generator package:
$ npm install -g express-generator
Run "$ express-V" to view the Express version information.
The reason for this is that after express4.0, the function of creating an app is separated into express-generator, and you cannot create an application without it.
NPM and module
The following two images of the express directory (Samba is installed and no pressure is displayed in Windows) can be used to compare the changes in the express package more intuitively. v3.15.0 is displayed on the left and v4.7.0 is displayed on the right:
A binfile is missing in the directory structure. The memory in this bin directory is placed with internal commands (), but they all exist in the form of independent files.
PS: Bin is short for binary. For the meaning of the bin and sbin directories, you need to have a little understanding of the Linux directory structure. You can find the corresponding information by using Google Baidu's "Linux directory.
The differences between modules (node_modules/Express/package. JSON) are as follows:
"bin": { "express": "./bin/express" }
Note that the information in the NPM module is in package. JSON, including various configuration information.
The "bin" Key indicates that the user can use the "command-name" command, and then runs the "command-file" file. As follows:
{ ... "bin": { "command-name” : “command-file” } ... }
Therefore, when entering the express command in the command line, the corresponding is actually to execute the express program under the bin directory.
If you want to create a module package and want to use the command line tool, you must configure the bin field. After global mode is installed, you can directly execute the package on the command line. For more information about package. JSON, see the configuration section. For more information, see "$ NPM help JSON.
Significance of global mode installation
NPM is installed in local mode and global mode, and "-G" indicates global mode, this mode will be installed in the node_modules folder in the lib directory of the node installation directory and used globally.
NPM is created by Isaac Z. the node Package Manager created by schlueter is provided to the node service. It needs to be installed independently at first. Later, when nodev0.6.3 integrates NPM as the node Package Manager by default, you can calculate the global package address installed with NPM.
For example, if node is in/usr/local/bin/node, the Module Directory is/usr/local/lib/node_modules.
If the installation mode is not global, the installation is in local mode. The installation mode is installed in the node_modules folder of the current directory and is not global.
What if express-generator is not installed in global mode?
What's really uncomfortable is that the command line tool can't be completed by directly using "express [Option] [dir]". It can only be executed manually by finding the execution file, for example, the local mode installation can only be executed in the current directory.
$ ./node_modules/express-generator/bin/express -V 4.2.0
Create a project
Ready has been completed in all aspects. Please create an Express project :)
Confirm the directory location.
Express MyApp
$ CD MyApp & NPM install
After NPM install is complete, run the following command:
$ DEBUG=myapp ./bin/www
The execution of "NPM start" has the same effect, which is the execution of the two statements:
PS: the content in the red box is printed during access.
We can see that there is a different line of information printed by the two commands. After "DEBUG = MyApp" is executed. /bin/www "will be printed:" MyApp Express server listening on port 8898 + 0ms"
Let's take a look and execute the command:
$ vi ./bin/www
PS: the default port is actually 3000 (the part in the red box). After you manually modify the port, port 8898 is used.
Express4 introduced the famous debug module, so the debug mode makes it easier for us to play node in the module, its use method is introduced for a long time, it is recommended that you look at its official introduction: https://www.npmjs.org/package/debug
The important package. JSON file will be introduced immediately. Let's talk about how to create the express4 project.
Directory structure
Understanding the directory structure is the first step to get started with all projects:
- App. JS, program Startup File
- Bin, mentioned when creating a project, is a real execution Program
- Node_modules: stores all project dependent libraries.
- Package. JSON, project dependency configuration and developer information
- Public, static files (CSS, JS, IMG)
- Routes, route File
- Views, page files
Configuration File magic package. JSON
In the nodejs project, package. the JSON file is used to declare the basic information and modules of the project. In this way, you only need. run the NPM install command in the directory where the JSON file is located.
The following example uses the package. JSON file to build the Express project for fun ).
Return to the project directory:
Add the testpackage project to the project directory.
$ mkdir testpackage && cd testpackage
Copy package. JSON from MyApp
$ cp ../myapp/package.json ./
Create a server. js file, write a few simple lines of code, and run one:
VaR express = require ('express '); var APP = Express (); app. get ('/', function (req, Res) {res. send ('Hello World') ;}); var Server = app. listen (8808, function () {console. log ('listening on port % d', server. address (). port) ;}); app. get ('/sayhi', function (req, Res) {res. send ('dog Black Dog feed ');});
The above two figures are the running results.
View the contents in package. JSON as follows:
In the meeting, we will build a quick project that is implemented by package. JSON. Let's take a look at its meaning:
- Name
~ The name and version fields are the most important in package. JSON. They are all required. If not, they cannot install;
~ Before thinking about the name, go to NPM registry to check whether the name has been used, http://registry.npmjs.org/
- Version
~ The version must be parsed by node-semver, which is included in the NPM dependency;
~ The identifier composed of name and version is unique in the hypothesis. Change the package version at the same time
- Private
~ If you set "private": True, NPM will not publish it;
~ This is a way to prevent accidental publishing of private libraries. If you want to confirm that the given package is only published in a specific registry (such as an internal Registry), use the description of publishconfighash to override the publish-time Configuration Parameter of the Registry.
- Scripts
~ "Scripts" is a hash object consisting of script commands, which are executed in different lifecycles of the package. Key is a lifecycle event, and value is a command to run.
- Dependencies
~ Dependency is a hash that specifies the version range for a group of package names. This version range is a string separated by one or more spaces. You can also use tarball or git URL for dependency.
The five items above are only used by express4 package. JSON. For other details, refer to the documentation portal https://www.npmjs.org/doc/files/package.json.html.
Summary
This article introduces NPM and module information at the application level, hoping to help you understand it.
We will introduce how to use express + MongoDB to build a personal website in the next chapter of the article in August. We hope that Lao Tzu will never skip the ticket :)
Materials
- Http://expressjs.com
- Http://www.npmjs.com/
- Https://www.npmjs.org/doc/files/package.json.html
- Http://mujiang.info/translation/npmjs/files/package.json.html
Express4 sailing Guide