Getting Started with Node.js Development (1)-Install Node.js and Editor Configuration

Node.js is an easy to build fast and extensible web application platform built on Chrome's JavaScript running. Node.js uses an event-driven, non-blocking I/O model, making it lightweight, efficient, and perfect data-intensive real-time applications running on distributed devices.

Node.js is a platform that allows JavaScript to run on the server without the browser, not a language; the Javascript engine used by Node.js is V8 from Google Chrome; it runs outside the browser without considering the headache of Javascript compatibility. It uses single-threaded, Asynchronous IO and event-driven design to achieve high concurrency (asynchronous events also increase the difficulty of development and debugging to a certain extent); Node.js has a built-in HTTP server, so it is good news for website development;

Installing Node.js on Windows is very convenient, we only need to visit the official website of node.js  http://www.nodejs.org/ , click the Download link, and then select Windows Installer (my machine is 64bit optional), and download the installation package . After the download is complete, double-click to install directly, just like other general software installations:



Choose the installation location:


Installation is complete: At


this point we have installed Node.sj, the following is the Node.js installation directory structure:


Start node



Find it in "Start"-"Programs" and


double-click node.js directly

Test a simple example: output "Hello, World!"

After we enter node, we can enter:

console.log("Hello,World!");

Then we will see the output on the command line: Hello, World!


In addition, you can also do this: In the installation directory of node.js, create a file named: hello.js, the code is as follows:


/** **/
	var sys = require("util");
	sys.puts("Hello world");

Next, let's test whether node.js can run correctly and open the node.js command prompt:

  

The execution is shown in the figure below:

 

The above is a simple Node.js program created using the command line, let's look at a more complicated application server program. For example, create a node folder under D:Program Files odejs, and then create a firstapp.js in it, open firstapp,js and enter the following code:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World - Node.js Work.
');
}).listen(5656, '127.0.0.1');
console.log('Server running at http://127.0.0.1:5656/');

Then open the command line, enter the node directory, and execute the command in the command line: node firstapp.js, then open the browser and enter the address in the browser: http://127.0.0.1:5656/ or http://localhost: 5656, you can run the code of the firstapp file.

The operation effect is as follows:

If there are no grammatical errors, that is the effect above.


Installation Environment:


(1) Install Node.js environment under Windows

You can download the installation package from the official website of Node at http://nodejs.org . I am Win7 64-bit. Visit the official website of Node. You can see that the latest version is 0.12.7. Click INSTALL to download the msi file, and then double-click to install it. Up.

Nodejs download

The installation process is very simple, select all the options and click Next.

msi will install npm (Node Package Manager) together, and will also help you set up environment variables, and add node, npm, etc. to the path, so you only need to open the command line window and you can work.

A Node.js directory will be added to the start menu, as shown below:

Node.js start menu item

Click the "Node.js command prompt" menu to enter the command line environment of Node.js, as shown in the following figure:

Node.js command line environment

Here you can use node and npm directly.

If you enter node and press Enter, you will enter the interactive environment of Node. You can enter some JavaScript commands to have a look, such as console.log("Hello Node.js!"). It's responsive. The effect of entering node is equivalent to clicking the Node.js menu in the start menu.

If you enter npm and press Enter, you will see the help of npm, as shown below:

npm help

That's it for the basic environment. It's very simple. Next, let's get nervous, go the old way, and have a look at HelloWorld.

HelloWorld website

The code is as simple as this:


   
    
    
  1. // Introduce http module
  2. var http = require( "http");
  3. // Create server and specify the function to process client requests
  4. http.createServer(
  5. function(request, response) {
  6. response.writeHead( 200, { "Content-Type": "text/plain"});
  7. response.write( "Hello World!");
  8. response.end();
  9. }
  10. ).listen( 8000);
  11. console.log( "Hello World is listening at port 8000");

Save it as HelloWorld.js, then under the command line environment of Node.js, enter the directory where HelloWorld.js is located, execute node HelloWorld.js, and the website can be up and running.

In our simple example, for any request, the text string "Hello World!" is returned. You can enter " http://localhost:8000 " in the browser to see the effect. I am here in Jiangzi:

view helloworld

Nothing special, ugly, right. For what the http module does, see here http://nodejs.org/dist/v0.12.7/docs/api/http.html .

Um, the magical world starts like this... There is a Node.js documentation sub-menu in the start menu, which can directly connect to the Node.js online documentation, and more exciting can start from there.


(2) Install Node.js environment under Linux (source code compilation and installation)

Linux installation of Node.js (source code compilation and installation)

environment:
Ubuntu 12.04.2 LTS (GNU/Linux 3.5.0-23-generic i686) to
download the Node.js installation package, please refer to the website: http://nodejs.org/download /

Select the source package installation method here. The installation process is as follows:

Log in to the Linux terminal and enter the /usr/local/src directory as follows:
root@ubuntu:~# cd /usr/local/src/

Download the nodejs installation package:
#wget http://nodejs.org/dist/v0.10.17/node-v0.10.17.tar.gz

2. Unzip the file and install
#  tar xvf node-v0.10.17.tar.gz 
#  cd node-v0.10.17 
#  ./configure 
# make 
# make install 
# cp /usr/local/bin/node /usr/sbin/ 

View the version of Node currently installed
# node -v

At

this point, the entire installation of v0.10.17 has been completed. If there are errors during the installation process, please refer to the following solutions: Possible problems: 

  1. The program 'make' is currently not installed.  You can install it by typing:    apt-get install make    
             


Follow its prompts and use the command 
            # apt-get install make
  1. g++: Command not found g++ has not been installed before, now execute the installation:    


#apt-get install g++

test program hello.js:
console.log("Hello World");
# node helloworld.js


Another example: WebServer,

a simple Web server written by Node, returns "Hello World" for each request response.
	var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World

');
}).listen(1337);
console.log('Server running at port 1337');

To run the server, write the code to the file example.js and execute the node program command line:



# node example.js

Server running at 
http://127.0.0.1:1337/









Friends who are interested can try the following example of a simple TCP server listening on port 1337 and responding:

	var net = require('net');
	var server = net.createServer(function (socket) {
	  socket.write('Echo server
');
	  socket.pipe(socket);
	});
	server.listen(1337, '127.0.0.1');


Introduction to NPM:

  

     The Node.js installation package for Windows includes Node Pageaged Modules  https://npmjs.org/  (npm). This node.js itself has basic modules. With this, npm can install rich node.js libraries to complete Actual development requirements.  

  Introduction to commonly used commands:

  View help

  npm helpnpm h

  Install the module

  npm intstall <Module Name>

  Install the module in the global environment (-g: enable global mode)

  npm install -g <Module Name>

  More: https://npmjs.org/doc/install.html

  Uninstall the module

  npm uninstall  <Moudle Name>

  Display the modules installed in the current directory

  above sea level a letter 

  After the installation is successful, the npm and node.js paths will be added to the PATH user environment variable and the system environment respectively

 

 

development tools:

     

     

     I downloaded WebStorm 7.0, it seems to be used a lot, WebStorm download address:  http://www.jetbrains.com/webstorm/

  For .NET development, you can choose WebMatrix3, download and install directly, the operation is very convenient, without too much explanation, after a period of time, you can directly build the Nodejs project, if you only develop in the Windows environment, it is recommended to use it

  WebMatrix download address:  http://www.microsoft.com/web/webmatrix/


  I finally chose Sublime, which can maintain a unified development tool under each platform. There are many configuration methods on the Internet.

  Sublime download address:  http://www.sublimetext.com/     (The software does not need to be registered, the registration window will pop up from time to time during use, just cancel it)

 

Sublime Node.js development environment configuration

  Download and install the Node.js installation package before starting configuration

  1. Install Sublime Text 2 first

  2. Run Sublime, find Tools on the menu ---> Build System ---> new Build System

  3. Copy in the file

{
    "cmd": ["node", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.javascript"
}

  4. Save the file as NodeJs.sublime-build

  5. Find Tools on the menu ---> Build System ---> select NodeJs

  6. Install Package Control; run Sublime, press the shortcut key Ctrl + `; enter the following in the console and press Enter;

import urllib2,os,hashlib; h = '7183a2d3e96f11eeadd761d777e62404' + 'e330c659d4bb41d3bdf022e94cab3cd0'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); os.makedirs( ipp ) if not os.path.exists(ipp) else None; urllib2.install_opener( urllib2.build_opener( urllib2.ProxyHandler()) ); by = urllib2.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); open( os.path.join( ipp, pf), 'wb' ).write(by) if dh == h else None; print('Error validating download (got %s instead of %s), please try manual install' % (dh, h) if dh != h else 'Please restart Sublime Text to finish installation')

  

  For details, please refer to: https://sublime.wbond.net/installation#st2

  7. After the installation is complete, restart Sublime, press the shortcut key Ctrl + Shift + P; enter install, select Install Package and press Enter

  

  8. Enter nodejs and select Nodejs installation (you can install JavaScript & NodeJs Snippets)

  

   9. Create a new test.js file, enter console.log('Hello Node.js'); Press the shortcut key Ctrl + B to run, and the output is successful! As shown below

  

  

 

  Configure the Sublime Node.js development environment here to succeed!

  If you want a better theme color scheme, please refer to: http://www.dbpoo.com/sublime-text2-theme-sod/

  

Extension: install multi-version manager

   Install the multi-version manager nvmw (The NVM used in the Node Development Guide book does not seem to work on Windows. I found this on www.npmjs.org. Although I can't use it for learning, I will install it first)

  Install command

  npm install -g nvmw

  Because I didn’t use it, I won’t do much introduction and check it in detail.

  https://npmjs.org/package/nvmw

Next time we will further analyze HelloWorld and introduce the basic program structure of nodejs and some knowledge points used by HelloWorld.

Node.js is an easy to build fast and extensible web application platform built on Chrome's JavaScript running. Node.js uses an event-driven, non-blocking I/O model, making it lightweight, efficient, and perfect data-intensive real-time applications running on distributed devices.

Node.js is a platform that allows JavaScript to run on the server without the browser, not a language; the Javascript engine used by Node.js is V8 from Google Chrome; it runs outside the browser without considering the headache of Javascript compatibility. It uses single-threaded, Asynchronous IO and event-driven design to achieve high concurrency (asynchronous events also increase the difficulty of development and debugging to a certain extent); Node.js has a built-in HTTP server, so it is good news for website development;

Installing Node.js on Windows is very convenient, we only need to visit the official website of node.js  http://www.nodejs.org/ , click the Download link, and then select Windows Installer (my machine is 64bit optional), and download the installation package . After the download is complete, double-click to install directly, just like other general software installations:



Choose the installation location:


Installation is complete: At


this point we have installed Node.sj, the following is the Node.js installation directory structure:


Start node



Find it in "Start"-"Programs" and


double-click node.js directly

Test a simple example: output "Hello, World!"

After we enter node, we can enter:

console.log("Hello,World!");

Then we will see the output on the command line: Hello, World!


In addition, you can also do this: In the installation directory of node.js, create a file named: hello.js, the code is as follows:


/** **/
	var sys = require("util");
	sys.puts("Hello world");

Next, let's test whether node.js can run correctly and open the node.js command prompt:

  

The execution is shown in the figure below:

 

The above is a simple Node.js program created using the command line, let's look at a more complicated application server program. For example, create a node folder under D:Program Files odejs, and then create a firstapp.js in it, open firstapp,js and enter the following code:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World - Node.js Work.
');
}).listen(5656, '127.0.0.1');
console.log('Server running at http://127.0.0.1:5656/');

Then open the command line, enter the node directory, and execute the command in the command line: node firstapp.js, then open the browser and enter the address in the browser: http://127.0.0.1:5656/ or http://localhost: 5656, you can run the code of the firstapp file.

The operation effect is as follows:

If there are no grammatical errors, that is the effect above.


Installation Environment:


(1) Install Node.js environment under Windows

You can download the installation package from the official website of Node at http://nodejs.org . I am Win7 64-bit. Visit the official website of Node. You can see that the latest version is 0.12.7. Click INSTALL to download the msi file, and then double-click to install it. Up.

Nodejs download

The installation process is very simple, select all the options and click Next.

msi will install npm (Node Package Manager) together, and will also help you set up environment variables, and add node, npm, etc. to the path, so you only need to open the command line window and you can work.

A Node.js directory will be added to the start menu, as shown below:

Node.js start menu item

Click the "Node.js command prompt" menu to enter the command line environment of Node.js, as shown in the following figure:

Node.js command line environment

Here you can use node and npm directly.

If you enter node and press Enter, you will enter the interactive environment of Node. You can enter some JavaScript commands to have a look, such as console.log("Hello Node.js!"). It's responsive. The effect of entering node is equivalent to clicking the Node.js menu in the start menu.

If you enter npm and press Enter, you will see the help of npm, as shown below:

npm help

That's it for the basic environment. It's very simple. Next, let's get nervous, go the old way, and have a look at HelloWorld.

HelloWorld website

The code is as simple as this:


   
  
  
  1. // Introduce http module
  2. var http = require( "http");
  3. // Create server and specify the function to process client requests
  4. http.createServer(
  5. function(request, response) {
  6. response.writeHead( 200, { "Content-Type": "text/plain"});
  7. response.write( "Hello World!");
  8. response.end();
  9. }
  10. ).listen( 8000);
  11. console.log( "Hello World is listening at port 8000");

Save it as HelloWorld.js, then under the command line environment of Node.js, enter the directory where HelloWorld.js is located, execute node HelloWorld.js, and the website can be up and running.

In our simple example, for any request, the text string "Hello World!" is returned. You can enter " http://localhost:8000 " in the browser to see the effect. I am here in Jiangzi:

view helloworld

Nothing special, ugly, right. For what the http module does, see here http://nodejs.org/dist/v0.12.7/docs/api/http.html .

Um, the magical world starts like this... There is a Node.js documentation sub-menu in the start menu, which can directly connect to the Node.js online documentation, and more exciting can start from there.


(2) Install Node.js environment under Linux (source code compilation and installation)

Linux installation of Node.js (source code compilation and installation)

environment:
Ubuntu 12.04.2 LTS (GNU/Linux 3.5.0-23-generic i686) to
download the Node.js installation package, please refer to the website: http://nodejs.org/download /

Select the source package installation method here. The installation process is as follows:

Log in to the Linux terminal and enter the /usr/local/src directory as follows:
root@ubuntu:~# cd /usr/local/src/

Download the nodejs installation package:
#wget http://nodejs.org/dist/v0.10.17/node-v0.10.17.tar.gz

2. Unzip the file and install
#  tar xvf node-v0.10.17.tar.gz 
#  cd node-v0.10.17 
#  ./configure 
# make 
# make install 
# cp /usr/local/bin/node /usr/sbin/ 

Guess you like

Origin blog.csdn.net/publicstaticfinal/article/details/114841728