文章目录
回顾与思考js的运行环境
浏览器是javascript的前端运行环境
 
什么是Node.js
它是一个基于Chrome V8 javascript的后端的运行环境,这也是为什么javascript可以用来写后端
 注意:Node.js无法调用DOM和BOM等浏览器内置API
 
模块化
moudle变量是一个对象,内有exports属性(moudle.export)是对外的接口- 误区提示:时刻谨记,
require()j加载模块时,得到的永远是moudle.exports指向的对象 
module.exports.age = 20
exports = {
    gender : 'nan',
    age :  22
}
module.exports = {
    username: '张三',
    gender: 'nan',
    age: 22
}
 
结果:
npm与包之i5ting_toc
i5ting_toc是可以将md文件转化为html文件的命令
-  
将
i5ting_toc安装为全局包npm install -g i5ting_toc -  
调用
i5ting_toc将md转化为html的功能i5ting_toc -f 要转换的md文件路径 -o 
中间件 next()函数
next()函数是多个中间件连续调用的关键,,如果当前中间件没有终结请求,并且next()没有被调用,那么请求将被挂起,后边定义的中间件将得不到被执行的机会。但有一个特例,如果我们定义的中间件终结了本次请求,那就不应该再调用next函数,即使,写完next()后面不应该有其他东西否则就可能会出问题,
// 必须包含 next
const express = require('express')
const { now } = require('moment')
const path = require('path')
const app = express()
// 中间件的使用:多个中间件可以共享同一份req res
// app.use((req, res, next) => {
//     console.log('easy')
//     // const time = Date.now()
//     // 为req对象挂载自定义属性,从而将获取时间的方法共享给后面的路由
//     // req.starttime = time
//     next()
// })
const m1 = (req,res)=> {
    const time = Date.now()
    console.log('easy');
    next()
}
app.get('/user',m1,(req, res) => {
    res.send('hello'+m1())
})
app.get('/',(req, res) => {
    res.send('world')
})
// 错误级别的中间件
app.get('/uuu', (req, res) => {
    throw new Error('服务器崩溃')
    res.send('hello')
})
app.use((err, req, res, next) => {
    console.log('错误' + err.message)
    res.send('Error'+err.message)
})
app.listen(80, () => {
    console.log('http://127.0.0.1');
})










