Preliminary study on Nodejs

Source: Internet
Author: User
Tags install node

Node overview

To NodeJS early have heard, but has been slow to do it, ha haha, today we will come to explore it.

Nodejs, what's the big stuff?

NodeJS, my understanding is that JavaScript can run on the backend.

Why is it able to run on the back end?

This is thanks to the V8 engine (V8, the JavaScript engine for Google Chrome), through the encapsulation of the high-performance V8 engine, and through a series of optimized API class libraries that enable it to run on the backend.

And node has two main features:

1, based on event-driven;

2, no blocking.

This makes the Nodejs ideal for processing concurrent requests.

We all know that the essence of Nodejs is that Javascript,so is not difficult to understand based on event-driven, but is not blocked?

JavaScript is a single thread, so to be non-blocking, node is through a number of callback functions to achieve this goal.

All right, no more talking nonsense. Then we begin to experience it.

Install NODE&NPM

Because Nodejs is a back-end JavaScript, so must have a running environment ah. However, the installation of the NODEJS environment is relatively simple, the specific steps are shown below:

1, first to the official website Download Nodejs installation package.

In the official website, you can also see a description of Nodejs:

node. js is a JavaScript runtime built on Chrome ' s V8 JavaScript engine. node. JS uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. node. js ' package ecosystem, NPM, was the largest ecosystem of open source libraries in the world.

2, after downloading the installation package, under Windows, follow the default program to go Ouke.

3, check whether the installation is successful, in the Windows environment, open a command prompt, enter node–v, if normal will appear the output of the version number.

As follows:

In the official presentation, it's not about npm(node. js ' package ecosystem, NPM, the largest ecosystem of open source libraries in the WOR LD. Well, in fact NPM when you install Nodejs, the area to help you install.

If you don't believe me, I'll just enter npm–v at the command prompt.

As follows:

Well, node development environment is so perfect ' build ' well, haha, isn't it very simple.

Next, let's start banging on the first node program.

Get started

Everything starts with "Hello World" , after all, we are people with feelings.

Simply put, we'll just hit our program at the command prompt, you just type node, and you can enter node in the interactive environment at the command prompt.

As follows:

After entering the node interactive environment, hit the "Hello World" string and enter.

The results are as follows:

If we want to quit this interactive environment of node, CTRL + C, and two times to exit back to the Windows command.

As follows:

If you want to program in this interactive environment of node, it's a lot of trouble, so we can introduce JS file to execute, such as the following.

premise: you have to have a JS file.

I put this JS (helloworld.js) file on the D drive, so the result is as follows:

' Use strict 'console.log (' Hello World ');
Helloworld.js

Ah, a little bit of meaning.

Not that Nodejs is a JavaScript running on the back end. Next, we'll use Nodejs to develop the simplest server program.

Node for server

Prerequisite:Nodejs is in accordance with the COMMONJS specification.

That

Each. js file is a module, the advantage of the module is to avoid namespace pollution. If you want to expose a module to external variables, you can use module.exports = variable;

And one module wants to refer to the variables exposed by another module, using the Require keyword, such as var ref = require (' module_name ');

Well, simply know the rules of Nodejs, then we want to write an HTTP server, we have to introduce this module first.

As follows:

The next step is to invoke a Factory mode method (Createserver) of the imported HTTP module to create a new HTTP server.

As follows:

Because one of the features of Nodejs is event-driven, so when we access an HTTP server, it triggers a request event that we use to handle it accordingly.

For example, we do this by using Writehead to set the HTTP response header and HTTP body.

The specific code is as follows :

Finally, the port number that you want to listen to. For example, we are listening to port 80.

Code and very perfect, but, in order to facilitate the NODEJS interactive environment to run the JS file, know that the server has been restarted, we still print a log bar.

As follows:

' Use strict '// the HTTP library is included in the program via require var http = require (' http '); // Create a new HTTP server var server = http.createserver (); // responding to request requests via the request event Server.on (' request ',function(req, res) {    Res.writehead ( {' Content-type ': ' Text/plain '});    Res.end (' Hell world\n ');    }); Server.listen (' n '); Console.log (' Server running! ');
Entirecode

OK, open the Nodejs interactive environment, run the JS file, my name is http.js.

So

So the HTTP server is ready, then we open the Web page, enter 127.0.0.1:80, to see the effect:

good!! There's a little bit of excitement in the wood, so the HTTP server is up and running.

Node is really cool, so let's study together.

Good night, everyone~.

Preliminary study on Nodejs

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.