Node.js
1. 基础知识
// require - 在当前模块中加载和使用别的模块,传入一个模块名,返回一个模块导出对象。
var foo1 = require('./foo');
var foo2 = require('./foo.js');// exports - 当前模块的导出对象,用于导出模块公有方法和属性。
// module - 通过 module 对象可以访问到当前模块的一些相关信息,但最多的用途是替换当前模块的导出对象。
const newModule = {
add: (a, b) => a + b
}
module.exports = newModule;2. 网络模块
2.1 Http 模块
const http = require('http')
const server = http.createServer(function (req, res) {
res.writeHead(200,{"Content-Type":"text/html;charset=UTF8"}); //插入响应头
if (req.url === '/index.html') {
res.write('This is demo.')
} else {
res.write('404')
}
res.end();
});
server.listen(3000, function () {
console.log('服务器启动成功了,可以通过 http://127.0.0.1:3000/ 来进行访问')
});2.2 URL 模块(解析 URL)
2.3 多进程
3. 文件模块
3.1 fs 模块
3.2 zlib 模块(压缩包)
4. Express 框架
4.1 快速入门
4.2 路由
方法
描述
4.3 中间件Middleware
最后更新于