nodejs-第一天,http/commonjs/npm

1. http服务器

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('hello world');
});

server.listen(port, hostname, ()=>{
    console.log(`server running at http://${hostname}:${port}/`);
});
1. 引入http模块
var http = require('http');
2. 创建服务
http.createServer(function (request, response){

})

createServer中函数的参数:
1. request 接收浏览器端传过来的信息
2. response 从服务器端向浏览器端输出内容
3. response.writeHead(); 请求头,包含状态码和Content-Type指明文件类型,编码格式等
4. response.write() 向浏览器写入的内容
5. response.end() 请求结束
6. 注意,若没有图标文件,会有两次请求,可以使用request.url !== '/favicon.ico'判断,剔除掉不想要的请求。

3. 监听端口

listen(端口号)

var http = require('http');

http.createServer(function (request, response){
    // request 接收浏览器端传过来的信息
    // response 从服务器端向浏览器端输出内容
    response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});

    if(request.url !== '/favicon.ico') { // 若是没有ico图标,会请求两次
        console.log('hello world');
        response.write('hello world'); // write()方法只是输出,客户端还在等待
        response.end(); // 结束
    }

}).listen('4000');
console.log('server listening at 4000');

2. 模块

Commonjs 规范

node调用借口例子:

1. 定义模块uitl.js
// 模块定义
var util = {
    sayHello: function(){
        return 'hello nodejs'
    },
    add: function(x, y){
        return x + y;
    }
};
// 模块接口的暴露
module.exports = util;
2.入口文件调用模块

入口文件commonjs.js

var http = require('http');
// 模块的调用
var util = require('./util');

http.createServer(function (request, response){
    response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
    if(request.url !== '/favicon.ico') { 
        // 模块方法的调用
        response.write(util.sayHello().toString()); 
        response.write(util.add(2,4).toString()); 

        response.end(); 
    }

}).listen('4000');
console.log('server listening at 4000');

1.通过qequire()调用自定义模块

var util = require('./util');

2.调用自定义模块的方法

util.sayHello();
module.exports另一种写法
exports.sayHello = util.sayHello;
exports.add = util.add;

调用时:

var sayHello = require('./util').sayHello;
var add = require('./util').add;

方法调用:

sayHello();
add(1,2);

3. NPM包管理工具

1.forever

让nodejs应用当成服务,在后台执行

$ sudo npm install forever -g   #全局安装
$ forever start app.js          #启动
$ forever stop app.js           #关闭
$ forever start -l forever.log -o out.log -e err.log app.js   #输出日志和错误
2.tree

查看当前目录文件
brew install tree

3.npm list

查看npm安装的模块

4.npm info XXX

查看指定模块信息,包括各种版本

5.npm 安装指定版本模块

npm i xxx@1.1.0

6.npm init 后端配置文件
{
  "name": "node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": { // 项目依赖

  },
  "devDependencies": { // 开发依赖,上线是,这个依赖就不需要

  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC"
}

"dependencies": { } 项目依赖
"devDependencies": { }开发依赖,上线是,这个依赖就不需要

7.npm install

执行npm install xxx –save 安装在dependencies,–save 可简写为-S
执行npm install xxx –save-dev 安装在devDependencies,–save-dev简写为 -D
例如:

cnpm i babel-cli --save-dev // 安装到devDependencies中

若只有package.json,只需要npm install,即可下载package.json中的模块

8.npm uninstall

可以加后缀 –save/–save-dev 移除package.json中的记录

9. 模块版本

"babel-core": "^6.0.0"
* * 安装最新版本
* ^ 插入符号会更新最新的主要版本(第一个数字)。^ 1.2.3将匹配任何1.xx版本,包括1.3.0,但会在2.0.0上停止。
* ~ 波浪符号与最的次要版本(中间号码)匹配。〜1.2.3将匹配所有1.2.x版本,但将错过1.3.0。

10. nrm NPM 源管理器

安装

npm i nrm -g

nrm ls查看源

 * npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
  taobao - https://registry.npm.taobao.org/
  nj ----- https://registry.nodejitsu.com/
  rednpm - http://registry.mirror.cqupt.edu.cn/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/

nrm test 速度测试

 * npm ---- 686ms
  cnpm --- 257ms
  taobao - 213ms
  nj ----- Fetch Error
  rednpm - 1373ms
  npmMirror  3134ms
  edunpm - Fetch Error

nrm use xxx 切换源

11.清除npm缓存

安装模块报错或很慢,退出安装。当继续安装时,有可能包没有覆盖,这时需要先卸载,再清除缓存

npm cache clean 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值