Node.js

121 阅读1分钟

是一个基于Chrome V8的JavaScript运行环境

终端中的快捷键

  1. 上方向键 ,可以快速定位上一次执行的命令
  2. 使用 tab键快速不全文件路径
  3. esc键快速清除当前命令
  4. cls快速清空终端所有内容

NPM

node 的包管理工具 可以下载依赖

**cnpm **国内镜像

cnpm install

模块化开发

1.require() 引入一个模块

const add = require("./addModule")

let result = add(10, 20);

console.log(result);

2.module.experts 暴露模块接口

核心模块

fs模块

用来操作文件的模块

1.读取指定文件中的内容

fs.readFile(filepath,[options编码格式],callback回调函数)

fs.writeFile(filepath,data[,options],callback)

path

处理文件路径 join方法链接两个路径

const path = require("path");

let domain = "http://www.xiaozhoubg.com"
let url = "docs";
let id = "22";

let result = path.join(domain,url,id);

console.log(result);

web 服务器
const http = require("http");

const server = http.createServer((req,res)=>{
    res.end("hello world");

})

server.listen(3000, ()=>{
    console.log('server is running');
})

http服务

导入模块

1.创建服务

2.监听一个端口 这里用8888

3.运行服务 node httpserver.js

4.在浏览器访问http://localhost:8888

路由

koa框架基础

nunjucks 模板引擎