Node. JS Series Basic Learning----installation, implementing Hello World, REPL

Source: Internet
Author: User
Tags install node

node. JS Basic Learning

1: Introduction

Simply put, node. JS is the JavaScript that runs on the server. node. JS is a platform built on the Chrome JavaScript runtime. node. JS is an event-driven I/O server-side JavaScript environment, based on Google's V8 engine, the V8 engine executes JavaScript very quickly and with great performance

2: Installation

à install node. js

Install your own win version of node. JS in the official website, download, after installation, enter NODE-V in the run if the version number appears to prove the success of the installation.

à install npm (module)

This NPM is a package management tool that is installed with node to solve the problem of node. JS code deployment. We can test directly through the run, npm-v

We need to install an express framework via NPM, we need to navigate to the Nodejs folder (the directory on My Computer), the use of DOS to operate, directly in the folder hold the shift right, run the command here on the line.

(Express is a simple and flexible node. js WEB application Framework that provides a range of powerful features to help you create Web applications, and a wealth of HTTP tools. Express can be used to quickly build a full-featured website. )

We can install this framework in the following form.

This creates the Node-modules folder under the Nodejs directory, and the third-party packages that we install locally will be in this folder.

?

I've installed 4 third-party packages,

*:body-parser This is the middleware for node. JS, which handles Json,raw,text and URL-encoded data.

*:cookie-parser This is a tool for parsing cookies, which can be used by req.cookies to get the cookie passed over and convert it to an object.

*: Also a middleware that handles MIME-encoded form data for the enctype= "Multipart/form-data" setup form.

    1. NPM Install Body-parser--save
    2. NPM Install Cookie-parser--save
    3. NPM Install Multer--save

As soon as we install our catalogue, we can directly require () in this project to introduce locally installed packages.

We can create a new product under the Nodejs directory to put our project in place, so that we can use a third-party package for multiple projects. There is no port conflict.

We can learn about the basics of node. js here. Http://www.runoob.com/nodejs

3 Run implementation: "Hello World"

Create a new Test.js file under Product, write the JS code inside the output can be achieved, but the output here we have to be in DOS, to watch.

?

We can create a new HTML page under this file, and then execute the node.test.js file, it will automatically run the server, we have a defined 9999 port, we can open the contents of the page.

The following is the code in JS, we put the HTML code back through JS.

  1. Here is the package that we need to use outside the call
  2. var express=require ("Express");
  3. var app=express ();
  4. Here are the two parameters, one is the request, the other is the response
  5. App.Get("/", function (request,response) {
  6. ?? Console.log (__dirname);
  7. ?? Response.sendfile (__dirname+ "/ahui.html"); //Accept the request and put it back to the client's HTML page
  8. })
  9. Define the service-side code, and * * See what value is put back
  10. var server=app.listen (9999,function () {
  11. var host=server.address (). Address;
  12. var port=server.address (). Port;
  13. Console.log (' Application instance, access address is http://%s%s ', host,port);//This is the code displayed on the server side
  14. });

I am here because there is no use of coding so it will appear garbled. Then we can open the Web page in the browser

It only accesses the server once, the server will show the path. That's the way it is. When we shut down the server, the content in the browser goes wrong.

4:node. js REPL(Interactive interpreter)

node. JS REPL represents a computer environment, similar to DOS, where we can enter commands and receive responses from the system.

Node comes with an interactive interpreter that can perform the following tasks.

à read: Read the user input, parse the input JS data structure and store in memory.

à execute: Executes the input data structure.

à print: output results.

à cycle: is the most basic cycle

Above these we can by two times press CTRL + C, to exit.

Note: There can be some simple operation, you can store the data in the variable, and use it when you need it, declare the need to use Var, if you do not use the VAR keyword variable will be printed directly, using var variable can be output by Console.log ().

REPL command

  • Ctrl + C?- exits the current terminal.
  • Ctrl + C Press two times ?- Exit Node REPL .
  • Ctrl + D?- exits Node REPL.
  • Upward / downward Key ?- View the history command of the input
  • Tab Key ?- List Current Commands
  • .help?- list the Use commands
  • .break?- exit multi-line expression
  • . Clear?- exit multi-line expression
  • . Save? filename? - Saves the current Node REPL session to the specified file
  • . Load? filename? - loads The contents of the file in the current Node REPL session.

?

Attachment:

Basic Learning: Http://www.runoob.com/nodejs

Blog: http://www.cnblogs.com/chuaWeb/p/nodejs-npm.html

Node. JS Series Basic Learning----installation, implementing Hello World, REPL

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.