Getting started with Node. js

Source: Internet
Author: User
Tags install node node server

What is Node. js? Node. js is a server-side programming framework based on googlev8 + javascript. But Node. js is not a js application. It should be said that it is a js running platform. It adopts event-driven, asynchronous programming and is designed for network services. Node. js has good performance. According to founder RyanDahl, the performance is Node. an important factor for js consideration. Selecting c ++ and v8 instead of ruby or other virtual machines is also based on performance. Node. js is also bold in design. It runs in single process and single thread mode, and the event-driven mechanism is Node. js is implemented through highly efficient maintenance of the event loop queue by a single internal thread. It does not have multi-threaded resource occupation and context switching, which means that in the face of large-scale http requests, Node. js handles everything by event-driven means. To use Node. js, you must first install it. Install Node. js installation environment: ubuntu11.10 32bit node-v0.8.16.tar.gz installation steps: first ensure that the system is installed in python, gcc, g ++, if not, install: [plain] view plaincopyprint? $ Sudo apt-get install python $ sudo apt-get installbuild-essential $ sudo apt-get install gcc $ sudo apt-get install g ++ $ sudo apt-get install python $ sudo apt-get installbuild-essential $ sudo apt-get install gcc $ sudo apt-get install g ++ download the latest source code package from the official nodeJS website http://nodejs.org/: node-v0.8.16.tar.gz unzip: [plain] view plaincopyprint? $ Tar-zxf node-v0.8.16.tar.gz $ cd node-v0.8.16 $ tar-zxf node-v0.8.16.tar.gz $ cd node-v0.8.16 default installation: [plain] view plaincopyprint? $./Configure $ make $ sudo make install $./configure $ make $ sudo make install select the directory method for installation: [plain] view plaincopyprint? $. /Configure-prefix =/usr/node $ make-j 5 #5 = number of CPU cores + 1 $ sudo make install $. /configure-prefix =/usr/node $ make-j 5 #5 = number of CPU cores + 1 $ sudo make install installation is complete. Run the following command to check the installed version: [plain] $ node-version v0.8.16 "Hello World". Let's get started with our first Node. js application: "HelloWorld ". Open your favorite editor and create a helloworld. js file. To do this, output "HelloWorld" to STDOUT. The code to implement this function is as follows: [html] console. log ("Hello World"); console. log ("Hello World"); save the file and use Node. js to execute: [html] view plaincopyprint? If node helloworld. js node helloworld. js is normal, HelloWorld will be output on the terminal. Well, I admit that this application is a little boring, so let's take a look at it ". A basic HTTP server starts with the server module. Create a server under the root directory of your project. and write the following code: [html] var http = require ("http"); http. createServer (function (request, response) {response. writeHead (200, {"Content-Type": "text/plain"}); response. write ("Hello World"); response. end ();}). listen (8888); var http = require ("http"); http. createServer (function (request, response) {response. writeHead (200, {"Content-Type": "text/plain"}); response. write ("Hello World" ); Response. end () ;}). listen (8888); done! You have just completed an HTTP server that can work. To prove this, we will run and test this code. First, use Node. js executes your script: [html] node server. js node server. next, open the browser to access http: // localhost: 8888/, and you will see a webpage with "HelloWorld. Now you are getting started. If you are interested in Node. js, you can find your own materials for further research.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.