Node: NodeJS installation and application configuration

I. Introduction

Node.js is running on the server side JavaScrip, NodeJS both to achieve a set of back-end development, but also with React and Mysql front-end to achieve a full stack development. So, learn about Node.js is still very necessary. I also began to learn to get started, we work together with it. 

 

Second, the installation

There are three nodeJS installation, are as follows:

1. Install by downloading the installation package official

// official website to download and install 
https://nodejs.org/en/download/

2, using the management tools Homebrew system installed in the mac os

// First, go to the official website of the Homebrew HTTPS: // brew.sh, copy the instructions to install Homebrew 
/ usr / bin / Ruby -e " $ (curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/ install) "

// Then, install the Node 
BREW install the Node

3, recommended nvm Manager version installed (nvm management nodejs and npm version)

// First, install nvm, you can view nvm of GitHub: https://github.com/nvm-sh/nvm 
curl-O-https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install. sh | bash

// Then, install the latest version of node 
nvm install

// NVM common functions 
NVM LS- Remote Official Version List the Node 
NVM stable install the latest stable version of the Node 
NVM install <Version> install the specified version of the 
NVM Uninstall <Version> Specifies the version to delete the installed 
NVM use <Version> switch uses the specified version the Node 
NVM LS lists all installed versions 
nvm current displays the current version of the 
NVM alias <name> <version> to a different version numbers add an alias 
NVM unalias <name> delete defined aliases 
NVM REINSTALL -packages <version> in the current version under node environment, the global re-install the specified version number npm package

 

Third, the application

After installing nodeJs, can now be put into node.js applied.

Application one: using node.js run a simple program

1. Create a project

mkdir node-demo

2, into the project

cd node-demo

3, npm initialization

// fast initialization 
npm the init - the y-

//package.json初始化的结果如下
{
  "name": "node-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

4, create a file

5, run the project

node src/app.js

// my terminal displays the result is: 4

 

Application 2: set up a web server using node.js

App.js modify the content is as follows:

// Web service
 // ajax ---> API ---> Web Server (node.js)

// Import module http 
const http = the require ( ' http ' );

// create a server instance
 @ REQ: requesting content
 @ RES: response content 
const Server http.createServer = ((REQ, RES) => {
    res.end('hello');
});

// server listens provided
 @ 3000: Listening port number
 @ 127.0.0.1: listening host
 // () => {}: listening callback 
server.listen ( 3000 , ' 127.0.0.1 ' , () => {
    the console.log ( ' server-initiated ' );
});

Results are as follows:

xiayuanquan @ XYQ  ~ / Desktop / Development Case / NodeApp / Demo  the Node the Node-src / app.js 
server startup

Browser results are as follows:

 

Guess you like

Origin www.cnblogs.com/XYQ-208910/p/12113409.html