You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varhttp=require("http");varurl=require("url");functionstart(){functiononRequest(request,response){varpathname=url.parse(request.url).pathname;console.log("Request for "+pathname+" received.");response.writeHead(200,{"Content-Type": "text/plain"});response.write("Hello World");response.end();}http.createServer(onRequest).listen(8888);console.log("Server has started.");}exports.start=start;
利用 url 模块,改造成一个具备路由功能的 server
新建一个 http.js 文件
varhttp=require('http')varurl=require('url')varstart=function(routeFn){http.createServer(function(req,res){constpath=url.parse(req.url).pathnameconsole.log('Request route is:'+path)routeFn(path,res)res.writeHead(200,{'Content-Type': 'text/plan'})res.write('hello, world')res.end()}).listen(8000)console.log('Server has started---')}exports.start=start
varhttp=require("http");varurl=require("url");functionstart(){functiononRequest(request,response){varpathname=url.parse(request.url).pathname;console.log("Request for "+pathname+" received.");response.writeHead(200,{"Content-Type": "text/plain"});response.write("Hello World");response.end();}http.createServer(onRequest).listen(8888);console.log("Server has started.");}exports.start=start;
varhttp=require('http');varurl=require('url');varutil=require('util');http.createServer(function(req,res){res.writeHead(200,{'Content-Type': 'text/plain; charset=utf-8'});res.end(util.inspect(url.parse(req.url,true)));// res.end(url.parse(req.url, true))// 报错:TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Url}).listen(3000);
Nodejs 是什么
为什么要使用 Nodejs
Nodejs 的优势
学习 Nodejs 前置知识
相关资料
安装 Nodejs
npm install cnpm -g
Nodejs REPL 环境
npm install npm -g
npm/cnpm install
npm/cnpm uninstall
什么是 回调,阻塞和非阻塞
阻塞式代码:
非阻塞式的代码:
Nodejs 事件驱动模型
观察者模式
核心: event Loop
Nodejs 模块化和模块加载方式
nodejs 模块加载流程
注意:文件缓存区和原生模块缓存不是同一个东西
./mod
或../mod
,相路径的文件模块(建议使用)/pathtomode/mod
绝对路径的文件模块hello 模块
main 模块
关于
module.exports = Hello, export.start
的区别和使用,推荐阅读资料:module.exports、exports、export、export default之间的关系和区别Nodejs 中的函数
匿名函数和具名函数
Nodejs 路由
参考:nodejs路由-菜鸟教程
了解下路由 模块 和简单实用:
利用 url 模块,改造成一个具备路由功能的 server
新建一个 http.js 文件
新建一个 route.js ,具备简单路由功能
新建一个 app.js
运行
node app.js
,浏览器输入对应的路由,即可预览效果Nodejs 的 GET 和 POST 请求
获取GET 请求 内容
使用:
util.inspect
是一个将任意对象转换 为字符串的方法,通常用于调试和错误输出。它至少接受一个参数 object,即要转换的对象。url.parse
解析 url 的参数为 url 实例对象,但是不能直接返回给浏览器访问
http://localhost:3000/user?name=Hj&url=www.test.com
页面结果:获取 URL 参数
使用
url.parse
来解析 url 的参数,代码为同样,输入
http://localhost:3000/user?name=Hj&url=www.test.com
,得到结果为获取 POST 请求内容
说明:POST 请求的内容全部都在请求体中,
http.ServerRequest
并没有一个属性内容为请求体原因:等待请求体传输是一件耗时的工作,比如上传文件等,避免 恶意的 post 请求占用cpu资源,nodejs 并不会处理post 请求体,需要手动处理。
需要用到知识点:
req.on('data')
监听 req 的 data 的事件监听函数,接收到请求体的数据,累加到 data 中req.on('end')
当 post 的请求体结束传输,触发该函数编写一个简单的 表单的例子
访问:
localhost:3000
,输入表单信息后,得到结果Nodejs 全局对象
Global Object 类似 window
__filename
__dirname
setTimeout
clearTimeout
setInterval
console
process
Nodejs 工具模块
用的比较少,更推荐使用 underscore.js
Nodejs 的文件系统
资料参考:Nodejs 文件系统-菜鸟教程
同步和异步
准备:
echo 'hello, world!' >> input.txt
异步数据在 回调中获取读取数据
打开文件
fs.open(path, flags[, mode], callback)
推荐学习资料:node.js fs.open 和 fs.write 读取文件和改写文件
关闭文件
fs.close(fd, callback)
回调只有 err 一个参数获取文件信息
fs.stat(path, callback)
异步获取文件信息读写文件
fs.writeFile(file, data[, options], callback)
fs.read(fd, buffer, offset, length, position, callback)
截取文件
fs.ftruncate(fd, len, callback)
异步模式截取文件,和异步读取文件方式类似,对比如下:fs.read(fd, buffer, offset, length, position, callback)
删除文件
fs.unlink(path, callback)
删除文件,callback 没有参数代码:
创建目录
fs.mkdir(path[, options], callback)
代码为:
读取目录
fs.readdir(path, callback)
删除目录
fs.rmdir(path, callback)
更多文件模块使用方法,参考:
The text was updated successfully, but these errors were encountered: