annewanghy / annewanghy.github.io

https://annewanghy.github.io
4 stars 0 forks source link

Node.js学习笔记 #17

Closed annewanghy closed 6 years ago

annewanghy commented 7 years ago
  1. 安装deepin系统(符合国人用户习惯的linux系统)

  2. 制作u盘启动盘

  3. 重启,按f12进入bash, 切换到startup启动项,将制作好的u盘调到首位

  4. 进入deepin安装界面, 选择安装盘

  5. 拔掉u盘, 重启, 完成安装

  6. 在deepin (linux)系统安装node.js 试了很多种方法, 最后发现nvm安装是最方便的 查看node.js的发行版本

    
    先安装一个 nvm( https://github.com/creationix/nvm )
    $ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.25.2/install.sh | bash
    nvm 的全称是 Node Version Manager,之所以需要这个工具,是因为 Node.js 的各种特性都没有稳定下来,所以我们经常由于老项目或尝新的原因,需要切换各种版本。

安装完成后,重启bash, 你的 shell 里面应该就有个 nvm 命令了,调用它试试 $ nvm 当看到有输出时,则 nvm 安装成功。

安装 Node.js 使用 nvm 的命令安装 Node.js 最新稳定版,现在是 v6.8.0。

$ nvm install 6.8.0 安装完成后,查看一下 why@why-PC:~/Downloads$ node -v v6.8.0 why@why-PC:~/Downloads$ npm -v 3.10.8 查看自己安装的所有 Node.js 版本 $ nvm ls -> v6.8.0 node -> stable (-> v6.8.0) (default) stable -> 6.8 (-> v6.8.0) (default) iojs -> iojs- (-> N/A) (default) #iojs是node.js的一个预发版本, 可以自己百度查看

如果出现下面错误,node未找到命令可以使用下面方法

why@why-PC:~/Downloads$ node bash: node: 未找到命令 why@why-PC:~/Downloads$ nvm use stable Now using node v6.8.0 (npm v3.10.8)

3. npm
在 PHP 中, 包管理使用的 Composer, python 中,包管理使用 easy_install 或者 pip,ruby 中我们使用 gem。而在 Node.js 中,对应就是 npm,npm 是 Node.js Package Manager 的意思。
设置npm为淘宝镜像

npm config set registry 'https://registry.npm.taobao.org'

或直接使用cnpm, 接下来每次都使用cnpm安装

npm install -g cnpm

4. 了解[`express` ](http://expressjs.com/en/starter/installing.html)

url

://:@:/ mkdir lesson1 & cd lesson1 npm install express --save-dev touch app.js // 引入express模块 var express = require('express'); // 调用express实例 var app = express(); //app本身有很多方法, 最常用的get, post, put/patch, delete // handler函数接收req, res两个参数, 分别试request请求和response会员 app.get('/', function(req, res) { res.send('Hello world!'); //res.send向屏幕输出一个字符 }) // 监听3000端口 app.listen(3000, function(){ console.log('app is listening at port 3000'); }) node app.js 打开浏览器,localhost:3000, 出现Hello Wolrd!字样 ``` 5. 使用`express-generator`快速生成express项目 ``` 全局安装express-generator npm install express-generator -g 快速生成项目 express --view=pug myapp create : myapp create : myapp/package.json create : myapp/app.js create : myapp/public create : myapp/routes create : myapp/routes/index.js create : myapp/routes/users.js create : myapp/views create : myapp/views/index.pug create : myapp/views/layout.pug create : myapp/views/error.pug create : myapp/bin create : myapp/bin/www create : myapp/public/javascripts create : myapp/public/images create : myapp/public/stylesheets create : myapp/public/stylesheets/style.css install dependencies: $ cd myapp && npm install run the app: $ DEBUG=myapp:* npm start > myapp@0.0.0 start /home/why/Downloads/myapp > node ./bin/www myapp:server Listening on port 3000 +0ms ``` route路由, get. post, put/patch, delete ``` Respond with Hello World! on the homepage: app.get('/', function (req, res) { res.send('Hello World!') }) Respond to POST request on the root route (/), the application’s home page: app.post('/', function (req, res) { res.send('Got a POST request') }) Respond to a PUT request to the /user route: app.put('/user', function (req, res) { res.send('Got a PUT request at /user') }) Respond to a DELETE request to the /user route: app.delete('/user', function (req, res) { res.send('Got a DELETE request at /user') }) ``` 6. `package.json` 和ultity.md5()加密 ``` npm init来生成一个package.json文件 npm install express utility --save 本地安装,会多出来依赖 "dependencies": { "express": "^4.15.3", "utility": "^1.12.0" } ``` http://localhost:4000/?q=st md5加密之后 627fcdb6cc9a5e16d657ca6cdef0a6bb ``` var express = require('express') var utility = require('utility') var app = express() app.get('/', function(req,res){ var q = req.query.q var md5value = utility.md5(q) res.send(md5value) }) app.listen(4000, function(req, res){ console.log('listening at port 3000') }) ``` 7.[ `superagent`](http://visionmedia.github.io/superagent/)和[`cherrio`](https://github.com/cheeriojs/cheerio)爬虫 superagent ``` request .post('/api/pet') .send({ name: 'Manny', species: 'cat' }) .set('X-API-Key', 'foobar') .set('Accept', 'application/json') .end(function(err, res){ if (err || !res.ok) { alert('Oh no! error'); } else { alert('yay got ' + JSON.stringify(res.body)); } }); ``` 爬取cnodejs网站的信息 ``` var express = require('express') var superagent = require('superagent') var cheerio = require('cheerio') app = express() app.get('/', function(req, res){ // 使用superagent爬取https://cnodejs.org/的内容 superagent.get('https://cnodejs.org') .end(function(err, content){ //命名为content是为了不与res冲突 if(err) { return next(err) } // 使用cheerio解析网页的html内容,与jquery类似 var $ = cheerio.load(content.text) var items = [] $('#topic_list .topic_title').each(function(idx, element) { var $element = $(element) items.push({ title: $element.attr('title'), href: $element.attr('href') }) }) res.send(items) }) }) app.listen(3001, function(req,res) { console.log('listening at port 3001') }) ``` `netstat -nap | grep node`查看node.js打开的端口 ``` tcp6 0 0 :::6000 :::* LISTEN 10958/node tcp6 4 0 :::3000 :::* LISTEN 8035/node tcp6 0 0 :::3001 :::* LISTEN 11004/node tcp6 3 0 :::4000 :::* LISTEN 9443/node tcp6 2 0 :::5000 :::* LISTEN 10849/node tcp6 1 0 127.0.0.1:4000 127.0.0.1:39482 CLOSE_WAIT 9443/node tcp6 1 0 127.0.0.1:5000 127.0.0.1:40180 CLOSE_WAIT 10849/node tcp6 1 0 127.0.0.1:5000 127.0.0.1:40182 CLOSE_WAIT 10849/node tcp6 491 0 127.0.0.1:5000 127.0.0.1:39340 CLOSE_WAIT 10849/node tcp6 1 0 127.0.0.1:5000 127.0.0.1:40178 CLOSE_WAIT 10849/node tcp6 0 0 127.0.0.1:3001 127.0.0.1:47786 ESTABLISHED 11004/node ``` 使用`kill -9 8035` 杀死进程, 其中8035是pid 11.Mongodb 与 Mongoose 的使用
annewanghy commented 7 years ago
why@why-PC:~/Downloads$ node
bash: node: 未找到命令
why@why-PC:~/Downloads$ nvm use stable
Now using node v6.8.0 (npm v3.10.8)
annewanghy commented 7 years ago

使用eventProxy

var eventproxy = require('eventproxy')
var superagent = require('superagent')
var cheerio = require('cheerio')

var url = require('url')
var cnodeUrl = 'https://cnodejs.org'

superagent.get(cnodeUrl)
    .end(function(err, res){
        if(err) {
            return next(err)
        }
        var topicUrls = []
        // 使用cheerio解析网页的html内容,与jquery类似
        var $ = cheerio.load(res.text)
        var items = []
        $('#topic_list .topic_title').each(function(idx, element) {
            var $element = $(element)
            var href = url.resolve(cnodeUrl, $element.attr('href'))
            topicUrls.push(href)
        })
        console.log(topicUrls)
    })
[ 'https://cnodejs.org/topic/597edd7f8f0313ff0d08d97a',
  'https://cnodejs.org/topic/58d0fb3517f61387400b7e15',
  'https://cnodejs.org/topic/58eee565a92d341e48cfe7fc',
  'https://cnodejs.org/topic/58ad76db7872ea0864fedfcc',
  'https://cnodejs.org/topic/592917b59e32cc84569a7458',
  'https://cnodejs.org/topic/5983dc3607cef83a61770a39',
  'https://cnodejs.org/topic/5983c6865f8478756117f705',
  'https://cnodejs.org/topic/57d68794cb6f605d360105bf',
  'https://cnodejs.org/topic/5981cbde07cef83a617709ab',
  'https://cnodejs.org/topic/594f6e21642874f845d9fe0d',
  'https://cnodejs.org/topic/5982ec5b07cef83a61770a08',
  'https://cnodejs.org/topic/5983c45b5f8478756117f703',
  'https://cnodejs.org/topic/5982c1fdc1eb855961dd60ef',
  'https://cnodejs.org/topic/5981b0db28607f916122dbd7',
  'https://cnodejs.org/topic/59827b8e5f8478756117f6ae',
  'https://cnodejs.org/topic/59813c92518c054e4fc0c377',
  'https://cnodejs.org/topic/5982e22a07cef83a61770a01',
  'https://cnodejs.org/topic/5982dd41c1eb855961dd610b',
  'https://cnodejs.org/topic/5982d6755f8478756117f6dd',
  'https://cnodejs.org/topic/5941f4959079357b642b2782',
  'https://cnodejs.org/topic/5982833d07cef83a617709d1',
  'https://cnodejs.org/topic/5982c924c1eb855961dd60f7',
  'https://cnodejs.org/topic/58caaec27dee71e5193a53ce',
  'https://cnodejs.org/topic/59813d88518c054e4fc0c37a',
  'https://cnodejs.org/topic/596c635927c8372819db0b84',
  'https://cnodejs.org/topic/59829644c1eb855961dd60ec',
  'https://cnodejs.org/topic/5981d74fc1eb855961dd60c1',
  'https://cnodejs.org/topic/597ca460e72077ff35fbbe7d',
  'https://cnodejs.org/topic/598132bd8f0313ff0d08da42',
  'https://cnodejs.org/topic/598267f307cef83a617709c5',
  'https://cnodejs.org/topic/59827e8428607f916122dc23',
  'https://cnodejs.org/topic/598271395f8478756117f6a9',
  'https://cnodejs.org/topic/5982728cc1eb855961dd60d7',
  'https://cnodejs.org/topic/57c68052b4a3bca66bbddbdd',
  'https://cnodejs.org/topic/5982791728607f916122dc1d',
  'https://cnodejs.org/topic/598045ec68aa87c774e5ee1e',
  'https://cnodejs.org/topic/5981ee8e07cef83a617709be',
  'https://cnodejs.org/topic/597995a5e72077ff35fbbd9e',
  'https://cnodejs.org/topic/57ce8a5e8039ce12476979af',
  'https://cnodejs.org/topic/598136bc8f0313ff0d08da48' ]

处理异步 eventproxy 提供了不少其他场景所需的 API,但最最常用的用法就是以上的这种,即:

var url = require('url') var cnodeUrl = 'https://cnodejs.org' var topicUrls = []

superagent.get(cnodeUrl) .end(function(err, res){ if(err) { return next(err) } var topicUrls = []

    // 使用cheerio解析网页的html内容,与jquery类似
    var $ = cheerio.load(res.text)
    var items = []
    $('#topic_list .topic_title').each(function(idx, element) {
        var $element = $(element)
        var href = url.resolve(cnodeUrl, $element.attr('href'))
        topicUrls.push(href)
    })
    console.log(topicUrls)
    var ep = new eventproxy()

    ep.after('topic_html', topicUrls.length, function(topics){

        // 开始行动
        topics = topics.map(function(topicPair){
            var topicUrl = topicPair[0]
            var topicHtml = topicPair[1]
            var $1 = cheerio.load(topicHtml)
            return ({
                title: $1('.topic_full_title').text().trim(),
                href: topicUrl,
                comment1: $1('.reply_content').eq(0).text().trim()
            })
        })

        console.log('final: ')
        console.log(topics)
    })

    topicUrls.forEach(function(topicUrl){
        superagent.get(topicUrl)
        .end(function(err, res){
            console.log('fetch '+topicUrl +' successful')
            ep.emit('topic_html', [topicUrl, res.text])
        })
    })
})
annewanghy commented 7 years ago

npm的替代品ied,like npm, but run faster

annewanghy commented 6 years ago

Node.js学习笔记 如何通过饿了吗Node.js面试 大前端的瑞士军刀,只记录有用的 Koa中文文档 Koa实战 前端 vue + 后端 koa,全栈式开发 bilibili 首页 Creating RESTful APIs with Express 4