Node5 服务器相关配置

const http = require('http')

let server = http.createServer()

server.on('request',(req,res)=>{
	res.end('hello')
})

server.listen(3000,'192.168.0.106',()=>{
	console.log('running...')
})
if(req.url.startsWith('/query') && req.method == "POST")
const http = require('http')
const path = require('path')
const fs = require('fs')

let readFile = (url,res) => {
	fs.readFile(path.join(__dirname,'www',url),'utf8',(err,fileContent)=>{
		if(err){
			res.end('server error')
		}else{
			res.end(fileContent)
		}
	})
}

http.createServer((req,res)=>{
	if(req.url.startsWith('/index')){
		readFile('index.html',res)
	}else if(req.url.startsWith('/about')){
		readFile('about.html',res)
	}else if(req.url.startsWith('/list')){
		readFile('list.html',res)
	}else{
		res.writeHead(200,{
			'Content-Type':'text/plain;charset=utf8'
		})
		res.end('页面被狗狗叼走了')
	}
}).listen(3000,()=>{
	console.log('running...')
})
const http = require('http')
const path = require('path')
const fs = require('fs')
const mime= require('./mime.json')

http.createServer((req,res)=>{
	fs.readFile(path.join(__dirname,req.url),(err,fileContent)=>{
		if(err){
			// 没有找到对应文件
			res.writeHead(404,{
				'Content-Type':'text/plain;charset=utf8'
			})
			res.end('页面被狗叼走了')
		}else{
			let dtype='text/html'
			// 获取请求文件的后缀
			let ext = path.extname(req.url)
			if(mime[ext]){
				dtype = mime[ext]
			}
			if(dtype.startsWith('text')){
				dtype+=';charset=utf8'
			}
			res.writeHead(200,{
				'Content-Type': dtype
			})
			res.end(fileContent)
		}
	})
}).listen(3000,()=>{
	console.log('running')
})
const http = require('http')
const path = require('path')
const fs = require('fs')
const mime= require('./mime.json')

exports.staticServer = (req,res,root)=>{
	fs.readFile(path.join(root,req.url),(err,fileContent)=>{
		if(err){
			// 没有找到对应文件
			res.writeHead(404,{
				'Content-Type':'text/plain;charset=utf8'
			})
			res.end('页面被狗叼走了')
		}else{
			let dtype='text/html'
			// 获取请求文件的后缀
			let ext = path.extname(req.url)
			if(mime[ext]){
				dtype = mime[ext]
			}
			if(dtype.startsWith('text')){
				dtype+=';charset=utf8'
			}
			res.writeHead(200,{
				'Content-Type': dtype
			})
			res.end(fileContent)
		}
	})
}



// 文件2
const ss = require('./xx.js')
http.createServer((req,res)=>{
	ss.staticServer(req,res,path.join(__dirname))
}).listen(3000,()=>{
	console.log('runnging...')
})
//get

/*
	get参数处理
*/
const url = require('url')

let str = 'http://www.baidu.com/abc/qqq?flag=123&keyword=java'
let ret = url.parse(str,true)
console.log(ret.query.keyword)

let obj = {
	protocol: 'http:',
	slashes: true,
	auth: null,
	host: 'www.baidu.com',
	port: null,
	hostname: 'www.baidu.com',
	hash: null,
	search: '?flag=123&keyword=java',
	query: 'flag=123&keyword=java',
	pathname: '/abc/qqq',
	path: '/abc/qqq?flag=123&keyword=java',
	href: 'http://www.baidu.com/abc/qqq?flag=123&keyword=java'
}

let ret1 = url.format(obj)
console.log(ret1)
//post

/*
	post参数处理
*/

const querystring = require('querystring')

// parse方法的作用就是把字符串转化成对象
// let param = 'username=list&password=123'
let param = 'foo=bar&abc=xyz&abc=123'
let obj = querystring.parse(param)

console.log(obj)

// stringify的作用就是把对象转换成字符串
let obj1 = {
	flag: '123',
	abc: ['hello','hi']
}
let str1 = querystring.stringify(obj1)
console.log(str1)