网上有很多关于 Node.JS 如何安装的教程, 我这里只是记录一下, 免得自己每次再找
一, 安装 nvm
在终端执行命令 curl -o- | bash
修改配置文件 ~/.bashrc , 在里面加入以下文字
- export NVM_DIR="$HOME/.nvm"
- [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
- [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion
让配置文件生效 source ~/.bashrc
二, 安装 node.JS
- nvm ls-remote
- nvm install v8.9.1
- nvm ls
- nvm use xx.xx
三, 新建项目
- mkdir firstProject
- cd firstProject
- NPM init
四, 写项目代码
例如我之前在 Node.JS 之使用 superagent + cheerio 来爬取网页内容 这篇笔记里面写了一段示例代码:
- var superagent = require('superagent');
- var cheerio = require('cheerio');
- var url = "http://xxx.xxx.com";
- var cookie = "locale=zh; sessionid=imq23m240knb3421b35j0x8q82nb8z7qb";
- var items = [];
- superagent.get(url)
- .set("Cookie", cookie)
- .end(function(error, res) {
- if (error) {
- throw error;
- }
- var $ = cheerio.load(res.text);
- $('.admin-table tbody tr').each(function (idx, value){
- $value = $(value);
- $value.find('td').each(function (iddx, book) {
- if (0 === iddx) {
- $book = $(book);
- $items.push($book.find('a').text());
- }
- });
- });
- console.log($items);
- });
将上面这段项目代码放到 index.JS 里面.(其实项目的入口文件可以不叫 index.JS , 可以叫其他任何名字如 App.JS 之类的. 只不过为了方面, 我习惯用 index.JS )
五, 安装项目所用到的模块
上面的代码中, 我用到了 superagent 和 cheerio , 那么在正式运行项目之前, 我要先安装这两个模块.
使用命令 NPM i superagent cheerio --save 安装需要的模块
六, 启动项目
node index.JS 就启动了项目
来源: http://www.qdfuns.com/article/51116/4719e3911aaa8e89d9ee48a9ebe02ba3.html