node.js1
第一部分:环境安装
1.下载安装nodejs
- http://www.nodejs.org/zh-cn/
- http://www.nodejs.org/en/
- msi版本的用来安装
- tar.gz版本用来配置webstorm
2.了解npm包管理工具
01.npm初始化
- npm init
- 文件夹名字不可包含大写字母
- 文件夹名字不可有空格
02.查看安装包
- npm ls(查看当地)
- npm ls -g (查看全局安装的模块)
03.下载bower包管理工具
- npm install bower -g
04.特定版本(bower info 包名)查看包的版本信息
- npm install body-parser@1.15.1 --save
- 加--save有两个作用
- 1.将下载的包的信息添加到package.json文件里
- 2.将下载下来包放到当前项目目录的node_modules
05.dev结点(只在开发时用到的模块信息放到dev节点)
- npm install body-parser --save-dev
06.批量拉包
- npm install ==>dependencies和devDependencies
- npm install --prod ==>dependencies
- npm install --only-dev ==>devDependencies
07.移除包
- npm uninstall packageName
- npm uninstall packageName -g
- (用bower必须要下载git)
第二部分:hello-world
1.在cmd或者git bash里运行:node 01.js
- 如果被占用,则把代理清除
- npm config set proxy null
- 通过npm下载web框架express
第三部分:基本组件1:console
1.console.log:打印日志
2.console.info:打印信息
3.console.warn:打印警告
4.console.error:打印错误
5.console.dir:打印信息,类型
6.console.time('tagName'):开始计时 ****
7.console.timeEnd('tagName'):结束计时 ****
8.console.trace():跟踪函数执行过程
9.console.assert(表达式):断言****
第四部分:HTTP
1.引入模块http
- 
require('http') var http=require('http');
2.创建服务器:createServer
(1)server=http.creatServer(function(req,res){});
        /*调用http模块方法创建一个服务器
        callback function 响应客户端请求的处理函数
        http.createServer(callback)
        */
        var server = http.createServer(function(req, res) {
            //req 来自客户端的请求对象
            //res 用于给客户端返回信息对象
            console.log("请求路径", req.url)
            res.end("来自服务器的响应信息")
        })
01.writeHead:响应头
- 
res.writeHead=object 
- 
200-配置 
- 
- text/html:以html形式返回
 
- 
- text.json:以json格式返回
 
- 
- text/xml:以xml格式返回
 
- 
- text/plain:原样返回
 
- 
- charset=utf-8
 
- 
6.'txt':'text/plain', 
- 
'html':'text/html', 
- 
'css':'text/css', 
- 
'xml':'application/xml', 
- 
'json':'application/json', 
- 
'js':'application/javascript', 
- 
'jpg':'image/jpeg', 
- 
'jpeg':'image/jpeg', 
- 
'gif':'image/gif', 
- 
'png':'image/png', 
- 
'svg':'image/svg+xml' 
- 
301- 
(2)监听:listen
01.server.listen(port,hostname,callback);
02.port:监听端口
03.hostname:监听主机名
04.callback:回调函数
- 
function(err) //开启服务器监听 /* port Number 端口号 host String 主机地址 callback function 监听开启成功后的回调函数 server.listen(port,host,callback) */ server.listen(3000, "127.0.0.1", function(err) { //err代表开启服务器监听过程中产生的错误信息,如果开启成功,值为null if (err) { //捕获上一步操作过程中产生的错误信息 console.log("服务器开始失败") throw err } console.log("服务器开启成功:http://localhost:3000") })
搭建服务器
    //引入express
    var path=require('path');
    var express=require("express");
    //用express模块创建一个服务器实例
    var server=express();
    //配置express服务器的静态文件目录
   //网站的静态文件   .html,.css,.js,.jpg,.png...
    server.use(express.static(path.join(__dirname,"www"))); //"./www"
    //开启服务器的监听
    server.listen(3000,function(err){
        if(err) throw err;
        console.log("server is running at http://localhost:3000");
    })
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号