node.js1

前端之家收集整理的这篇文章主要介绍了node.js1前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<h1 id="第一部分环境安装">第一部分:环境安装
<h3 id="下载安装nodejs">1.下载安装nodejs

  • http://www.nodejs.org/zh-cn/
  • http://www.nodejs.org/en/
  • msi版本的用来安装
  • tar.gz版本用来配置webstorm

  • npm init
  • 文件夹名字不可包含大写字母
  • 文件夹名字不可有空格

  • npm ls(查看当地)
  • npm ls -g (查看全局安装的模块)

  • npm install bower -g

  • npm install body-parser@1.15.1 --save
  • 加--save有两个作用
  • 1.将下载的包的信息添加到package.json文件
  • 2.将下载下来包放到当前项目目录的node_modules

  • npm install body-parser --save-dev

  • npm install ==>dependencies和devDependencies
  • npm install --prod ==>dependencies
  • npm install --only-dev ==>devDependencies

  • npm uninstall packageName
  • npm uninstall packageName -g
  • (用bower必须要下载git)

  • 如果被占用,则把代理清除
  • npm config set proxy null
  • 通过npm下载web框架express

错误">4.console.error:打印错误

Metagname开始计时">6.console.time('tagName'):开始计时 ****

函数执行过程">8.console.trace():跟踪函数执行过程

  • require('http')

      var http=require('http');

          /*调用http模块方法创建一个服务器
    
      callback function 响应客户端请求的处理<a href="/tag/hanshu/" target="_blank" class="keywords">函数</a>
    
      http.createServer(callback)
      */
      var server = http.createServer(function(req,res) {
          //req 来自客户端的请求对象
          //res 用于给客户端返回信息对象
          console.log("请求路径",req.url)
    
          res.end("来自服务器的响应信息")
    
      })</code></pre>

    <h5 id="writehead响应头">01.writeHead:响应头

  • res.writeHead=object
  • 200-配置

    1. text/html:以html形式返回
    1. text.json:以json格式返回
    1. text/xml:以xml格式返回
    1. text/plain:原样返回
    1. 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-{'Location':'新地址'}

    函数">04.callback:回调函数
  • function(err){console.log(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服务器的<a href="/tag/jingtaiwenjian/" target="_blank" class="keywords">静态文件</a>目录

//网站的静态文件 .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");
})</code></pre>

猜你在找的Node.js相关文章