node.js 服务器
http服务器搭建
const http = require('http');//引入http模块
const server=http.createServer(function(req,res){
if(req.url=='/'){
res.end('返回数据')
}
}).listen(4000,function(){//开启服务器监听
console.log("http://127.0.0.1:4000\nserver is running");
})
get请求
const http = require('http');//引入http模块
const server=http.createServer(function(req,res){
const path=req.url.split('?')[0]
var parse=req.url.split('?')[1]
if(path=='/getData'&&req.method=='GET'){
console.log('get提交参数:'+parse);
res.end('返回get请求数据数据')
}
}).listen(4000,function(){//开启服务器监听
console.log("http://127.0.0.1:4000\nserver is running");
})
post请求
const http = require('http');//引入http模块
const server=http.createServer(function(req,res){
const path=req.url.split('?')[0]
var parse=''
if(path=='/postdata'&&req.method=='POST'){
req.on('data',function(data){
parse+=data //流数据传输
})
console.log('post提交参数:'+parse);
res.end('返回post请求数据数据')
}
}).listen(4000,function(){//开启服务器监听
console.log("http://127.0.0.1:4000\nserver is running");
})