# node简介
> 简单的说 Node.js 就是运行在服务端的 JavaScript。
> Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台。
> Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好。
# nodeJS安装配置
>node中文网:首先配置node环境 在cmd的dos下执行node -v,查看node是否安装成功
> gitbash : 最好装个git,在当前文件夹下,右键选择 git bash here即可在里面输入命令
# api
> 使用 require 指令来载入 Node.js 模块。
> 1、创建一个名为node文件夹
> 2、node文件夹下创建一个名为app的js文件 app.js
> 3、node文件夹下创建一个名为view文件夹,里面新建一个index.html
> 4、node文件夹下创建一个名为ceshi文件夹
> 5、每次测试写完代码测试,在node文件夹下cmd的dos下执行 node app
```api
var fs = require('fs')
var path = require('path')
var http = require('http')
```
## fs (文件系统)
```
λ 写入 ls
<!-- 往当前目录下的ceshi文件夹下,同步写入一个ceshi.txt文件,文件内容为helloWorld,字节编码格式为utf8 -->
<!-- res为返回值无特定含义,如果写入成功返回的是undefined -->
var resWrite = fs.writeFileSync('./ceshi/ceshi.txt' , 'helloWorld' , 'utf8')
console.log(resWrite)
<!-- 往当前目录下的ceshi文件夹下,异步写入一个ceshi1.txt文件,文件内容为helloWorld,字节编码格式utf8可以省略 -->
<!-- 无返回值,但有一个回调函数,可以在回调函数判断写入成功与否-->
fs.writeFile('./ceshi/ceshi1.txt' , 'helloWorld1' , function(err) {
if(err){
console.log('写入失败')
throw err
}
console.log('写入成功')
})
```
```
λ 添加 ls
<!-- 往当前目录下的ceshi文件夹下,同步写入一个log.txt文件,添加的文件内容为---,字节编码格式为utf8 -->
<!-- 他与writeFileSync的区别是一个是写入并替换,一个在原有内容下进行添加内容,可以用来做后台日志 ,当文件夹下没有log.txt文件,都会新创建一个-->
fs.appendFileSync('./ceshi/log.txt' , '这是添加的内容' + new Date(), 'utf8')
<!-- 往当前目录下的ceshi文件夹下,异步写入一个log.txt文件,文件内容为helloWorld,字节编码格式utf8可以省略 -->
fs.appendFile('./ceshi/log.txt' , '\n 这是异步的数据' + new Date(), function(err){
if(err){
console.log("添加失败")
throw err;
}
console.log("添加成功")
})
```
```
λ 读取 ls
<!-- 同步读取当前目录下的ceshi文件夹下,一个叫做ceshi.txt文件,字节编码格式为utf8 -->
<!-- resRead作为返回值是 ceshi.txt文件里的数据-->
var resRead = fs.readFileSync('./ceshi/ceshi.txt' , 'utf8');
console.log(ceshi2)
<!-- 异步读取当前目录下的ceshi文件夹下,一个叫做ceshi.txt文件,字节编码格式为utf8 -->
<!-- 无返回值,data 是 ceshi.txt文件里的数据-->
fs.readFile('./ceshi/ceshi.txt' , 'utf8' , function(err,data) {
console.log(data)
if(err) {
console.log('读取失败')
throw err
}
console.log('读取成功')
})
```
```
判断文件是否存在
<!-- 同步判断的ceshi文件夹下,是否有一个叫做ceshi.txt文件-->
<!-- resExist作为返回值 为返回的数据是否存在,存在resExist为true 否则的话resExist为false-->
var resExist = fs.existsSync('./ceshi/ceshi.txt')
console.log(resExist)
<!-- 同步判断的ceshi文件夹下,是否有一个叫做ceshi.txt文件 -->
<!-- data为返回的数据是否存在,存在data为true 否则的话data为false-->
fs.exists('./ceshi/ceshi1.txt', function(data){
console.log(data)
})
```
```
监听文件是否发生改变
<!-- 监听ceshi文件夹下,一个叫做ceshi1.txt文件-->
<!-- 输入node app发现它不动,此时修改ceshi1.txt文件里的内容,然后就会输出-->
<!-- newVal为改动之后的文件返回,oldVal是改动之前的文件返回-->
fs.watchFile('./ceshi/ceshi1.txt',function(newVal,oldVal){
console.log(newVal,"new")
console.log(oldVal,"old")
})
```## path(处理文件路径操作)
```
<!-- 路径拼接,拼接为www\baidu\com -->
var ownpath = path.join('www','baidu','com')
console.log(ownpath);
console.log(__dirname)//当前文件夹路径 自主参数
console.log(__filename)//当前文件路径
```
## http(服务)
```
//请求 响应变量
//创建服务
var myServer = http.createServer(function(req,res) {
var myurl = ''
//req.url是请求的路径
myurl = req.url == '/' ? '/index.html' : req.url;
var mypath = path.join('./view',myurl)
if(fs.existsSync(mypath)) {
var html = fs.readFileSync(mypath)
res.write(html)//响应写入
}else {
var errhtml = fs.readFileSync('./view/err/404Error.html')
res.write(errhtml)//响应写入
}
console.log(mypath,"测试数据")
res.end()//响应结束
})
//创建监听一个端口号3000
myServer.listen('3000' , function(err) {
if(err){
console.log('监听失败')
throw err
}
console.log('服务器已开启端口号为3000')
})
```