2018-12-07
const http=require('http');
const fs=require('fs');
//querystring:post:uname=jack&upwd=123
const queryString=require('querystring');
//url:get: http://www.baidu.com/index?uname=jack&upwd=123
const urlLib=requier('url');
var server=http.createServer(function(req,res){
var file_name='./www'+req.url;
//写文件:
fs.writeFile('文件名','内容',function(err){
if(err){
res.write('404');
}
})
//读文件
fs.readFile('文件名',function(err,data){
if(err){
res.write('404');
}else{
res.write(data);
}
})
res.write('fggfggb');
var url=req.url;// /?uname=jack&upwd=123
queryString.parse('una')//var uname=jack&upwd=123
var result=urlLib.parse('http://www.baidu.com/index?uname=jack&upwd=123',true);
console.log(result.query);//uname=jack&upwd=123
console.log(result.pathname); //?index
});
server.listen(8080);
-
// 下载:npm install express
const express=requie('express');
2.
// cnpm install express-static
const expressStatic=require('express-static');
5.
const bodyParser=require('body-parser');
//cookie-parser
const cookieParser=require('cookie-prase');
//cookie-session
const cookieSession=require('cookie-session');
var server=express();
server.use(bodyParser.urlencoded());
server.use(cookieParser())
server.use(cookieSession({
keys:['asads','fvgfd','dfgfdg'];
}))
3.
server.use('/1,html',function(req,res){
res.send('fdfdfdb');
res.end();
4.
console.log(req.query);
5.
console.log(req.body);
})
6.
server.use('/',function(req,res,next){
console.log('a');
next();
});
server.use('/',function(req,res){
console.log('b');
})
server.use('/',function(req,res){
res.cookie('user','jack')
})
server.use('/',function(req,res){
console.log(req.cookies);
res.send('200');
})
server.use('/',function(req,res){
res.secret='fdbgdfdfbfdgf';
res.cookie('user','jack',{signed:true})
res.send('200');
});
//session
server.use('/',function(req,res){
console.log(req.session)
})
server.listen(8080);
server.use(expressStatic('./www'))