This article describes the construction of the node. JS development environment under MACOSX.
Directory:
1. Introduction
2. Build the Environment
3. Development
4. References
1. Introduction
node. JS is a platform built on the chrome JavaScript runtime to easily build responsive, easy-to-scale Web applications. node. js is lightweight and efficient with event-driven, non-blocking I/O models and is ideal for data-intensive, real-time applications running on distributed devices.
Node is a JavaScript run environment (runtime). It actually encapsulates the Google V8 engine. The V8 engine executes JavaScript very fast and performs very well. Node optimizes some of the special use cases, providing an alternative API that allows V8 to run better in a non-browser environment.
2. Build the Environment
Use NVM to install and manage the node. js version. In the terminal, enter:
Brew Install NVM
To modify the ~/.bash_profile file, add the following:
Export Nvm_dir=~/.nvmsource $ (brew--prefix NVM)/nvm.sh
Restart terminal, and you can use NVM to install node. js.
NVM LS-remote //view the node. JS version that can be installed
NVM Install XXX version //install XXX version of NODE.JSNVM ls//view already installed node. JS version nvm use XXX version//using the XXX version of node. js
NVM alias default XXX version //Make sure that there is a defaults version
3. Development3.1. A simple node. JS program.
new Hello.js file:
var http = require ("http"); Http.createserver (function (request, response) { Response.writehead (200, {") Content-type ":" Text/html "}); Response.Write ("Hello world!"); Response.End ();}). Listen (8080); Console.log ("Server running at http://localhost:8080/");
Execute node hello.js in terminal and use a browser to access http://localhost:8080/to implement a simple HTTP server.
3.2. Using the express framework to develop node. JS Applications
Use the NPM command to manage the dependency packages for node. js. Enter NPM help in terminal to see the relevant commands. Since NPM uses foreign sources by default, it is slow to access the walls at home. You can use the Taobao NPM image instead.
1. Via the config command:
NPM Config Set Registry https://registry.npm.taobao.orgnpm info underscore (if configured correctly this command will have the string response)
2. Command line designation
NPM--registry https://registry.npm.taobao.org Info underscore
3. Edit ~/.NPMRC Add the following:
Registry = https://registry.npm.taobao.org
NPM Common commands are as follows:
NPM Install <name> //Install Nodejs's dependency package npm install <name>-G //pack the package into the global environment. But in the code, the direct way through require () is that there is no way to call a globally installed package. The global installation is for use by the command line, as if Vmarket was installed globally, you can run the VM command directly on the command line NPM install <name>--save //install and write the information to Package.json.
If you have a Package.json file in the project path, you can install all of the dependent packages directly using the NPM install method based on the dependencies configuration NPM init //will guide you to create a Package.json file, including the name , version, author, etc. npm remove <name>//Remove NPM update <name>//update NPM LS//List all packages currently installed
Express is a fast, open, minimalist web development framework based on the node. JS platform.
The latest version of Express will be installed by the NPM install Express--save//NPM installation Express, or you can install the specified version by adding the version number later, such as NPM install [email Protected
NPM Install express-generator-g //express Project Build Tool
After the installation is complete, create a new Express project and enter it in Terminal:
Express <name> //created with the Jade template by default
CD <name>
NPM Install//Installation Express project dependent
NPM Start//Run Express project
Enter Http://localhost:3000/to access the project in the browser.
3.3.Supervisor watchdog tool
in developing a node. JS program, when debugging, you need to restart the service, no matter what part of the code you are modifying. can take effect. This is because node. js only parses the script when it is first referenced to a part , and then accesses the memory directly, avoiding repeated loading. This design of node. JS, while improving performance, is not conducive to development debugging because we always want to see the effect as soon as we change it, rather than terminating the process and restarting it every time. Supervisor can help you implement this feature by monitoring your code changes and automatically restarting node. js. The principle is to listen to the changes in the file, once the change, restart the service.
Installing Supervisor
NPM Install Supervisor-g
Supervisor--help//View supervisor how to use
3.4. Development tools
Recommended Webstorm.
4. References
http://npm.taobao.org/
http://my.oschina.net/robinjiang/blog/168732
http://blog.csdn.net/lcstrive/article/details/25244593
node. JS Development Environment Building