您的位置:首页 > Web前端 > Node.js

《Node.js简记》 安装Node.js并实现Helloworld

2017-05-15 19:17 495 查看

前言:

之前利用Github的Pages功能开发我的个人博客的时候,使用了
Hexo
框架是基于
Node.js
开发的静态网页框架,然后最近在开发跨平台桌面应用的时候,使用了
Electron
框架的核心,也是基于
Node.js
(接下来都简称为:Node)的,想来有必要梳理一番。

Node.js简介:

什么是Node:

Node.js
是运行在服务端的JavaScript,其核心是与
Google Chrome
一样的
V8
引擎,是一个基于事件驱动的异步IO架构。简单地说,就是为在服务端运行JavaScript提供一个运行时环境,使得JavaScript得以在运行端被执行起来。

V8 JavaScript
引擎是 Google 用于其 Chrome 浏览器的底层
JavaScript 引擎
,是用来解释和执行JavaScript代码的运行时环境

服务端JavaScript:

JavaScript
最早是运行在浏览器中,然而浏览器只是提供了一个
上下文
,它定义了使用JavaScript可以做什么,但并没有太多关于JavaScript语言本身可以做什么。事实上,JavaScript是一门“完整”的语言: 它可以使用在不同的上下文中,其能力与其他同类语言相比有过之而无不及。

Node.js事实上就是另外一种上下文,它允许在后端(脱离浏览器环境)运行JavaScript代码。

要实现在后台运行JavaScript代码,代码需要先被解释然后正确的执行。Node.js的原理正是如此,它使用了Google的
V8虚拟机
(Google的Chrome浏览器使用的JavaScript执行环境),来解释和执行JavaScript代码。

Node安装:

开发平台可以选择
Linux
Mac
Windows
,当然安装的方法也稍有不同,
Linux
Mac
都是基于
Unix
操作系统演变而来,所以两者有很多相似之处,
Windows
环境下则大不相同。

1.安装之前请先安装并配置好
git
工具:

在命令行窗口或者终端输入:
git --version
,如下则为安装完成:

C:\Users\Administrator>git --version
git version 2.11.0.windows.1


2.Linux或Mac下的安装:

使用
nvm
(全称是:Node Version Manager)来安装和管理Node的安装和版本更新:

安装
nvm


通过
git
nvm
克隆到本地:

git clone https://github.com/cnpm/nvm.git[/code] 
假如安装一切顺利,应当如下:

linsh@ubuntu:~$ mkdir git
linsh@ubuntu:~$ cd git/
linsh@ubuntu:~/git$ git clone https://github.com/cnpm/nvm.git 正克隆到 'nvm'...
remote: Counting objects: 3928, done.
remote: Total 3928 (delta 0), reused 0 (delta 0), pack-reused 3928
接收对象中: 100% (3928/3928), 971.87 KiB | 3.00 KiB/s, done.
处理 delta 中: 100% (2292/2292), done.
检查连接... 完成。


配置终端启动时自动执行nvm:

~/.bashrc
,
~/.bash_profile
,
~/.profile
, 或者
~/.zshrc
文件添加以下命令:

source ~/git/nvm/nvm.sh    #这里 ~/git 是上一步骤存放nvm的目录,按照自己的实际情况进行修改


例如这里我修改的是
/etc/bash.bashrc
,然后添加以上内容:

sudo vi /etc/bash.bashrc


设置完毕之后重新打开一个终端,然后输入
nvm
,配置成功可以看到:

linsh@ubuntu:~$ nvm

Node Version Manager

Note: <version> refers to any version-like string nvm understands. This includes:
- full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
- default (built-in) aliases: node, stable, unstable, iojs, system
- custom aliases you define with `nvm alias foo`

Usage:
nvm help                              Show this message
nvm --version                         Print out the latest released version of nvm
nvm install [-s] <version>            Download and install a <version>, [-s] from source. Uses .nvmrc if available
--reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>
nvm uninstall <version>               Uninstall a version
nvm use [--silent] <version>          Modify PATH to use <version>. Uses .nvmrc if available
nvm run <version> [<args>]            Run <version> with <args> as arguments. Uses .nvmrc if available for <version>
nvm current                           Display currently activated version
nvm ls                                List installed versions
nvm ls <version>                      List versions matching a given description
nvm ls-remote                         List remote versions available for install
nvm version <version>                 Resolve the given description to a single local version
nvm version-remote <version>          Resolve the given description to a single remote version
nvm deactivate                        Undo effects of `nvm` on current shell
nvm alias [<pattern>]                 Show all aliases beginning with <pattern>
nvm alias <name> <version>            Set an alias named <name> pointing to <version>
nvm unalias <name>                    Deletes the alias named <name>
nvm reinstall-packages <version>      Reinstall global `npm` packages contained in <version> to current version
nvm unload                            Unload `nvm` from shell
nvm which [<version>]                 Display path to installed node version. Uses .nvmrc if available

Example:
nvm install v0.10.32                  Install a specific version number
nvm use 0.10                          Use the latest available 0.10.x release
nvm run 0.10.32 app.js                Run app.js using node v0.10.32
nvm exec 0.10.32 node app.js          Run `node app.js` with the PATH pointing to node v0.10.32
nvm alias default 0.10.32             Set default node version on a shell

Note:
to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)


使用
nvm
安装Node的指定版本:


可以使用
nvm
来安装node的任何版本,这里以最新的
6.10.3
为例:

nvm install 6.10.3


出现下载进程知道完成:

linsh@ubuntu:~$ nvm install 6.10.3

######################################################################## 100.0%

WARNING: checksums are currently disabled for node.js v4.0 and later
Now using node v6.10.3 (npm v3.10.10


查询是否安装成功:

linsh@ubuntu:~$ node -v
v6.10.3


3.Windows下安装:

使用
nvmw
来安装和管理Node的安装和版本更新:

安装
nvmw


通过
git
nvm
克隆到本地:

git clone https://github.com/cnpm/nvmw.git[/code] 
克隆完成,结果如下:

E:\Node\tools\gits>git clone https://github.com/cnpm/nvmw.git Cloning into 'nvmw'...
remote: Counting objects: 292, done.
Receiving objects:  79%remote: Total 292 (delta 0), reused 0 (delta 0), pack-reu
Receiving objects:  80% (234
Receiving objects: 100% (292/292), 52.74 KiB | 10.00 KiB/s, done.
Resolving deltas: 100% (165/165), done.


配置nvmw的路径到系统环境变量Path:

这里我的
nvmw
的安装目录是
E:\Node\tools\gits\nvmw
,将其添加到系统变量中:

E:\Node\tools\gits>set "Path=%Path%;E:\Node\tools\gits\nvmw;"


测试
nvmw
是否安装成功:


在命令行窗口输入:
nvmw


E:\Node\tools\gits>nvmw

Node Version Manager for Windows

Usage:
nvmw help                          Show this message
nvmw install [version] [arch]      Download and install a [version]
for [arch] architecture (optional)
nvmw uninstall [version]           Uninstall a [version]
nvmw use [version]                 Modify PATH to use [version]
nvmw ls                            List installed versions

Example:
nvmw install v0.10.21        Install a specific version number of node.js
nvmw use v0.10.21            Use the specific version
nvmw install iojs            Install the latest version of io.js
nvmw install iojs-v1.0.2     Install a specific version number of io.js
nvmw use iojs-v1.0.2         Use the specific version io.js

nvmw install v0.10.35 x86    Install a 32-bit version


其他的与在Linux或者Mac系统下几乎一致,这里不再赘述。这里多说一句,想要学习服务器的开发,最好还是选择Linux操作系统来进行开发工作,Node作为一个服务端的框架,肯定也是推荐使用Linux为最优选择。

Helloworld:

新建测试脚本:

新建一个
.js
的脚本命名为
helloworld.js


linsh@ubuntu:/application/node$ sudo vi helloworld.js


输入一个测试log输出的语言:

console.log("hello world");


运行测试脚本:

打开cmd窗口,定位到上面脚本所在的目录,然后使用node来运行该脚本:

linsh@ubuntu:/application/node$ node helloworld.js
hello world


至此Node的安装就已经完成了。

推荐入门资料:

Node入门

Node.js 入门

参考资料:

Node.js 究竟是什么?

快速搭建 Node.js / io.js 开发环境以及加速 npm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: