HTTP,URL,FS模块
- HTTP模块
const http = require('http');//加载http模块
http.createServer(function (req,res) {
res.end('Hello World\\n');}).listen(3000,"192.168.119.128"); //创建Server
console.log("Server running");
- FS模块
const http = require('http'); //加载http模块
const fs = require('fs');//加载fs模块
const ip = '192.168.119.128;//定义常量ip
const port = 3000;//定义常量端口号
var data = fs.readFileSync('/etc/passwd');//读取本地文件/etc/passwd,并赋给data变量
var f = function (req,res) { res.write(data);//输出data数据到浏览器 res.end(); }
var f2 = function (){ console.log('Server start');}//后台执行http.createServer(f).listen(port, ip,f2);//创建服务`
- URL模块
const http = require('http');//加载http模块
const url = require('url');//加载url模块
const ip = '192.168.119.128';//定义常量ip
const port = 3000;//定义常量端口号
var f = function (req,res) {
var pathname = url.parse(req.url).pathname;//获取客户端url地址 res.write(pathname+'\\n');//输出客户端url地址
res.end();//结束请求}
var f2 = function (){ console.log('Server start');}//定义后台显示函数
http.createServer(f).listen(port, ip,f2);//创建server
作业
构造静态服务Server:
解析URL,根据URL读取返回不同文本。