


How to use node.js? Let's talk about the application process of node.js in a simple way:
大家知道node.js的用法吗?到底是怎么用的,这篇文章全文通俗的讲了关于node.js的应用过程,让大家都会使用node.js,现在让我们一起来看这篇文章吧
首先我们来说说node.js的使用方法:
简单的说 Node.js 就是运行在服务端的 JavaScript。
Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台。
Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好。
node.js的安装配置:
Node.js安装包及源码下载地址为:https://nodejs.org/en/download/。选择Windows 安装包(.msi)运行即可
检查是否安装成功:DOS窗口 输入 node -v 或者 node --version 检查node版本;npm -v 检查npm版本
使用npm来安装node的模块
NPM的全称是Node Package Manager,是随同NodeJS一起安装的包管理和分发工具,它很方便让JavaScript开发者下载、安装、上传以及管理已经安装的包。
命令:
npm install <module name> 例如: npm install express -g //安装express模块,-g代表全局安装
全局安装的默认位置一般位于:C:\Users\用户名\AppData\Roaming\npm
但是现在出现了问题,不管我们安装什么模块,总会报一长串的错误,仔细看错误都是和网络有关,原因是npm的配置出了问题,由于伟大的墙导致我们只是使用npm的原始的registry地址下载很慢,我们可以配置淘宝NPM镜像地址,方法:
npm config set registry ="https://registry.npm.taobao.org/"//这一项不是必须的
但是设置完镜像之后,再次安装node模块还是出错,原因还是网络问题,这时候你就应该想到代理的问题了,使用公司的电脑访问网络需要设置相应的代理,设置npm的代理:
npm config set proxy="http://用户名:密码@192.168.16.189:8080"//@后面的是主机和端口
修改全局路径:
//npm的配置文件一般在你电脑的对应用户目录下面搜索: .npmrc 可以找到此配置文件 npm config set prefix "D:\\nodejs\\node_modules\\npm\\node_cache"//设置下载的模块的存储位置,也就是在全局安装模式下安装的模块的位置 npm root -g //查看npm全局安装目录 npm config set cache "D:\\nodejs\\node_modules\\npm\\node_global_modules" //设置缓存的存储位置
随着以后下载的模块越来越多,我们需要重新设置模块的下载位置和缓存的存储位置 (学node.js推荐上PHP中文网,那里有node.js视频教程)
使用 package.json
每一个下载的模块都有一个package.json文件,package.json 位于模块的目录下,用于定义包的属性。接下来让我们来看下 express 包的 package.json 文件,位于 node_modules/express/package.json 内容:
Package.json 属性说明:
name - 包名。
version - 包的版本号。
description - 包的描述。
homepage - 包的官网 url 。
author - 包的作者姓名。
contributors - 包的其他贡献者姓名。
dependencies - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。
repository - 包代码存放的地方的类型,可以是 git 或 svn,git 可在 Github 上。
main - main 字段是一个模块ID,它是一个指向你程序的主要项目。就是说,如果你包的名字叫 express,然后用户安装它,然后require("express")。
keywords - 关键字
node.js的版本号:
使用NPM下载和发布代码时都会接触到版本号。NPM使用语义版本号来管理代码,这里简单介绍一下。
语义版本号分为X.Y.Z三位,分别代表主版本号、次版本号和补丁版本号。当代码变更时,版本号按以下原则更新。
如果只是修复bug,需要更新Z位。
如果是新增了功能,但是向下兼容,需要更新Y位。
如果有大变动,向下不兼容,需要更新X位。
说了这么多,我们通俗的来讲一下node.js的应用:
打开安装目录 找到 node.exe 运行就可以看到类似DOS的窗口----REPL 的命令行窗口
> x = 10 10 > var y = 10 undefined > x + y 20 > console.log("Hello World") Hello World undefined > console.log("www.runoob.com") www.runoob.com undefined
应用Node.js程序
首先要使用express创建一个项目
使用npm安装Express开发框架:
//命令行输入命令 npm install -g express 或者 npm install -g express-generator
新建项目
//命令行输入命令 express -t ejs newsproject //使用express的创建命令创建一个名为newsproject的项目
如果此处出现意外错误:‘express’ 不是内部或外部命令,也不是可运行的程序或批处理文件。
原因是版本问题:当前版本是4.0.0,改成3.5.0即可运行,在cmd中输入下面命令
npm install -g express-generator@3.5.0 或 npm install -g express@3.5.0
成功安装express之后
进入项目目录,运行npm安装
/
/进入项目目录 cd newsproject //打开项目目录 //输入命令安装 npm install //npm安装
运行项目
//项目目录下运行命令 node app.js //运行此项目
到此为止,我们已经使用express框架自动搭建了一个node服务器,并启动了express提供的一个demo应用,我们可以按照cmd中的提示修改下面的端口号,使用浏览器访问即可得到demo应用的响应。
浏览器访问:http://127.0.0.1:3000/即可见nodejs站点页面,页面输出:Express
好了,以上就是这篇关于node.js的用法介绍详情了(想学这些就来看PHP中文网的Node.js开发手册),有问题可以在下方留言。
The above is the detailed content of How to use node.js? Let's talk about the application process of node.js in a simple way:. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
