Toggle navigation


Nodejs镜像手册

Version 1.0.0

本文档适用于Linux和Windows下的Node.js镜像

Node.js是一个Javascript运行环境(runtime)。 实际上它是对Google V8引擎进行了封装。V8引 擎执行Javascript的速度非常快,性能非常好。Node.js对一些特殊用例进行了优化,提供了替代的API,使得V8在非浏览器环境下运行得更好。
Node.js是一个基于Chrome JavaScript运行时建立的平台, 用于方便地搭建响应速度快、易于扩展的网络应用。Node.js 使用事件驱动, 非阻塞I/O 模型而得以轻量和高效,非常适合在分布式设备上运行数据密集型的实时应用。

镜像安装完成后,服务已经自动启动。使用浏览器打开首页demo:http://IP地址,首页正常显示,则表示环境安装成功。

如果安装后,不能访问首页,到管理控制台重启一下服务器,待服务器启动完全完成后再次打开首页。(原因:镜像安装后,首次启动服务没有自动启动,重启一般就解决了,以后重启也都会自动启动)

本章是Linux环境下的配置和使用说明

软件目录和版本

软件安装在/data/nodejs*(*表示nodejs不同的版本号,以下不再单独说明)。

首页对应目录:/data/nodejs*/apache2/htdocs,该目录包含首页index等文件。

版本:Apache 2.4.20 、Node.js 6.2.2、Python 2.7、Redis 3.2.1

服务启动停止

服务随操作系统自动启动,如果修改配置参数后,需要手工启停服务。

[root@iZ94bojtvkpZ nodejs-6.2.2]# cd /data/nodejs-6.2.2
[root@iZ94bojtvkpZ nodejs-6.2.2]# ./ctlscript.sh -h
usage: ./ctlscript.sh help
./ctlscript.sh (start|stop|restart|status)
./ctlscript.sh (start|stop|restart|status) apache
./ctlscript.sh (start|stop|restart|status) redis

help – this screen
start – start the service(s)
stop – stop the service(s)
restart – restart or start the service(s)
status – show the status of the service(s)

举例:

[root@iZ94bojtvkpZ nodejs-6.2.2]# ./ctlscript.sh restart
Syntax OK
/data/nodejs-6.2.2/apache2/scripts/ctl.sh : httpd stopped
/data/nodejs-6.2.2/redis/scripts/ctl.sh : redis stopped
/data/nodejs-6.2.2/redis/scripts/ctl.sh : redis started
Syntax OK
/data/nodejs-6.2.2/apache2/scripts/ctl.sh : httpd started at port 80

Redis

If you find any problem starting Redis, the first place you should check is
the Apache error log file:

/data/nodejs*/redis/var/log/redis-server.log.

Most errors are related to not being able to listen to the default port.
Make sure there are no other server programs listening at the same port
before trying to start Redis. For issues not covered in this Quick Start guide, please refer to
the Redis documentation, which is located at http://redis.io/documentation

本章是Linux环境下的配置和使用说明

软件目录和版本

软件安装在C:websoft9nodejs*(*表示nodejs不同的版本号,以下不再单独说明)。

首页对应目录:C:websoft9nodejs*apache2htdocs,该目录包含首页index等文件。

版本:Apache 2.4.20 、Node.js 6.2.2、Python 2.7、Redis 3.2.1

服务启动停止

服务随操作系统自动启动,如果手工修改配置参数后,需要重新启停服务,有两种方法。

方法一:在“开始”-> “管理工具”->“服务”中重启nodejsstackApache服务。

方法二:在C:websoft9nodejs*中找到“manager-windows”。

双击打开“manager-windows”,可以进行服务启停、参数配置。

Redis

If you find any problem starting Redis, the first place you should check is
the Apache error log file:

C:/websoft9/nodejs*/redis/var/log/redis-server.log.

Most errors are related to not being able to listen to the default port.
Make sure there are no other server programs listening at the same port
before trying to start Redis. For issues not covered in this Quick Start guide, please refer to
the Redis documentation, which is located at http://redis.io/documentation

Linux:

[root@iZ94bojtvkpZ nodejs-6.2.2]# cd /data/nodejs-6.2.2/
[root@iZ94bojtvkpZ nodejs-6.2.2]# ./use_nodejs                          #进入nodejs命令行环境
bash-4.2#

 

Windows:进入C:websoft9nodejs-6.2.2目录,双击执行use_nodejs,进入nodejs命令行环境。

 

然后,就可以进行如下操作了:

—————————————————————————————————————————

How to install modules?

In these cases, the command varies slightly:

$ npm -g install PACKAGE

For example, to install Express, you would execute:

$ npm -g install express

This will make the Express command-line available in all projects.

 

How to create a project using Express?

Installing it is as simple as starting the Bitnami console and then executing the command below:

$ npm -g install express
$ npm -g install express-generator

Create a skeleton project by executing these commands. This will produce a new application in the projects/sample-application directory:

$ cd installdir
$ mkdir -p projects
$ cd projects 
$ express sample-application

Then, install dependencies using npm:

$ cd sample-application
$ npm install 

This will download and install the required dependencies into a node_modules directory under your project folder.

How to start the Express web application  ?

To start your application, you just have to start the Bitnami console and then execute the following command, inside your application directory:

$ node app.js

If you are using a recent Express version (greater than 4.0), you should use the following commands instead:

$ ./bin/www (Linux and Mac OS X native installers)
> node binwww (Windows native installer)

Port 3000 is the default port used by Express when creating the template. This means that you may receive an error similar to the below when starting the server:

Error: listen EADDRINUSE
    at errnoException (net.js:670:11)
    at Array.0 (net.js:771:26)
    at EventEmitter._tickCallback (node.js:190:38)

This means that the port is already in use. Edit the app.js script and change the line below:

...
app.listen(3000, function(){
...

Specify a different port number, as in the example below:

...
app.listen(4000, function(){
...

Then, start the application again.

 

How to list the installed modules?

To list the modules installed locally in a project, enter the project directory and execute the npm list command, as shown in the example below. Remember to start the Bitnami console first if using a native installer.

$ cd sample-application
$ npm list
application-name@0.0.1 /home/bitnami/projects/sample-application
├─┬ express@2.5.8 
│ ├─┬ connect@1.8.7 
│ │ └── formidable@1.0.9 
│ ├── mime@1.2.4 
│ ├── mkdirp@0.3.0 
│ └── qs@0.4.2 
└─┬ jade@0.26.1 
  ├── commander@0.5.2 
  └── mkdirp@0.3.0 

You can also list the globally installed packages by entering the project directory and executing the npm -g list command, as shown below:

$ cd sample-application
$ npm -g list
/opt/bitnami/nodejs/lib
├── coffee-script@1.3.3 
├── colors@0.6.0-1 
...
├─┬ express@2.5.9 
│ ├─┬ connect@1.8.7 
│ └── qs@0.4.2 
│ ├─┬ cliff@0.1.7 
...
│ │ ├── colors@0.6.0-1 
├─┬ jade@0.26.0 
│ ├── commander@0.5.2 
│ └── mkdirp@0.3.0 
├── node-dev@0.2.3 
├─┬ npm@1.1.21 
│ ├── abbrev@1.0.3 
│ ├─┬ node-gyp@0.4.1 
│ │ ├── ansi@0.0.4 
│ ├── uid-number@0.0.3 
│ └── which@1.0.5 
└── uglify-js@1.2.6 


How to upgrade NPM?

You can upgrade npm to the latest version with the following command:

$ npm -g install npm

Installing Node.js applications?

NOTE: To avoid a conflict between two or more Node.js applications attempting to use the same port, modify the application settings as needed to use a different port for each application.

To install a Node.js application, first ensure that all dependences are satisfied. Many applications will require Node.js and MongoDB, but others may require Redis or MySQL. To check available services, run this command:

installdir/ctlscript.sh status

To install an application in the example/ directory, first create the directory:

mkdir installdir/apps/example
Then, move the application files to that directory and follow the official installation steps.