Mac下配置node.js的开发环境

JavaScript012

Mac下配置node.js的开发环境,第1张

## 一、搭建本地开发环境

1、[安装HomeBrew](http://brew.sh/index_zh-cn.html)

安装命令

>/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

卸载命令

>ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

2、[使用HomeBrew安装node.js](http://nodejs.cn/download/package-manager/#osx)

3、安装express

>npm install -g express

>

>npm install -g express-generator

4、安装WebStorm

[WebStorm 11](https://pan.baidu.com/s/1c1PeBao)(2016.1)

破解方法:启动选择License server,输入“http://idea.qinxi1992.cn”。

5、[使用HomeBrew安装并启动MongoDB](http://blog.csdn.net/fsw0723/article/details/17040999)

#### 注意事项及相关报错:

1、通过 express -V 查看express版本号

express版本号小于4.0,使用 node app 运行。

express版本号大于4.0,使用 npm start 或 node bin/www 运行

2、brew install mongodb 报错“Error:Permission denied - /usr/local/var”

在“/usr/local/”下创建文件夹“var”即可

## 二、配置服务端

1、[配置并启动服务器](https://aws.amazon.com/cn/?sc_channel=PS&sc_campaign=acquisition_CN&sc_publisher=baidu&sc_medium=brandzone&sc_content=pc&sc_detail=title&sc_category=pc&sc_segment=101&sc_matchtype=exact&sc_country=CN)

2、连接服务器

1)、使用密钥连接

>ssh -i ~/.ssh/server.pem ec2-user@{公共ip}

2)、使用密码登陆,如没有开启密码登陆方式,参考三

>ssh ec2-user@{公有ip}

3、启用密码登陆方式

1)、创建root密码:

>sudo passwd root

2)、切换root身份,并编辑sshd_config文件

>su root

>

>vim /etc/ssh/sshd_config

搜索PasswordAuthentication no,把no改为yes。

3)、重启sshd

>/sbin/service sshd restart

4)、给用户ec2-user添加密码

>passwd ec2-user

5)、新开一个窗口,验证登录密码

>ssh ec2-user@{公有ip}

4)、安装node.js、npm

>git clone https://github.com/nodejs/node.git

>

>cd node

>

>git tag -l

>

>git checkout v6.3.0

>

>./configure

>

>make

>

>sudo make install

5、安装express

修改sudoers文件

>su root

>vim /etc/sudoers

找到“secure_path”,然后添加node、npm的安装路径“:/usr/local/bin”

安装express、express-generator

>sudo npm install -g express

>

>sudo npm install -g express-generator

5)、[安装、启动mongodb](https://docs.mongodb.com/master/tutorial/install-mongodb-on-amazon/?_ga=1.67701784.882863290.1470383187)

#### 注意事项:

1、启动亚马逊服务器之前,先选择地区,默认是美国东部的佛吉尼亚北部(建议选择亚太区的)。启动之后不可更改。

2、WebStorm程序默认使用端口3000,建议直接开启服务器的端口3000。

## 三、服务端运行

1、用WebStorm创建一个NodeJs Express App的项目

不需要写什么,空的就好。

2、上传到服务端,介绍四种上传方式。

+ 用WebStorm连接服务端

第一步、Tools->Deployment->configruation

第二步、配置Connection

![image](http://upload-images.jianshu.io/upload_images/2477695-37378cc996619eb8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

第三步、配置Mappings

![image](http://upload-images.jianshu.io/upload_images/2477695-cc0bb70d28406924.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

+ 通过git push到oschina、bitbucket等,再通过git pull到服务器

只需要在Mac端、服务端安装git即可。推荐[Git@OSC](https://git.oschina.net/),可免费创建100个项目。

+ 在亚马逊云服务器配置GitLab

[服务端配置GItLab,选择“CentOS 6”](https://about.gitlab.com/downloads/#centos6)

安装完成在浏览器输入服务器地址,根据提示输入root密码。如果出现502错误,检查服务器8080端口是否开启。亚马逊配置的Gitlab不是很稳定,不推荐。

+ 使用ftp工具

每次上传都是全部,速度很慢,不推荐。

3、运行并查看效果

终端连接服务器,并进入项目目录,输入node app,然后在浏览器输入服务器地址,打开“Express Welcom to Express”界面。(WebStorm工程端口号默认3000)

可以为生产环境和开发环境使用不同的配置。

Node.js 假定其始终运行在开发环境中。 可以通过设置 NODE_ENV=production 环境变量来向 Node.js 发出正在生产环境中运行的信号。

通常通过在 shell 中执行以下命令来完成:

但最好将其放在的 shell 配置文件中(例如,使用 Bash shell 的 .bash_profile),否则当系统重启时,该设置不会被保留。

也可以通过将环境变量放在应用程序的初始化命令之前来应用它:

此环境变量是一个约定,在外部库中也广泛使用。

设置环境为 production 通常可以确保:

You can use conditional statements to execute code in different environments:

For example, in an Express app, you can use this to set different error handlers per environment:

文章来源 node中文官方 http://nodejs.cn/