博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nodejs之参数的接收GET 和POST
阅读量:4288 次
发布时间:2019-05-27

本文共 3600 字,大约阅读时间需要 12 分钟。

req.url:

Url {

  protocol: null,

  slashes: null,

  auth: null,

  host: null,

  port: null,

  hostname: null,

  hash: null,

  search: '',

  query: {},

  pathname: '/one',

  path: '/one',

  href: '/one' }

=====================get提交方法,接收参数

*******创建服务器

var http = require('http');var fs = require('fs');var url = require('url');var routes=require('./models/routes');http.createServer(function (request, response) {   var pathname = url.parse(request.url).pathname;if(pathname!="/favicon.ico"){  pathname=pathname.replace(/\//,"");// 吧/去掉  try{      routes[pathname](request,response);//根据路径分发路由,执行相应的程序  }catch(err){    console.log("错误"+err);    response.writeHead(200,{"Content-Type":"text/html"});    response.write(err.toString());    response.end("");  }console.log("主程序执行完毕");   }}).listen(8000);console.log('Server running at http://127.0.0.1:8000/');

********************route路由***********

var url = require('url');

url.parse(req.url,true).query;
var optfile=require("../models/optFile");var url=require('url');function getRecall(req,res){    res.writeHead(200, {'Content-Type': 'text/html'});  function recall(data){    res.write(data);//  发送响应数据,在网页上显示    res.end("ok");// 如果没有这句话,则网页一直在加载,不完成;就没有http协议尾部  }  return recall;}module.exports={login:function(req,res){var rdata=url.parse(req.url,true).query;//******************console.log(rdata);if(rdata["email"]!=undefined){console.log(rdata['email']);}  //创建一个闭包,作为参数传过去recall=getRecall(req,res);// optfile.readfileSync("D:nodejs/views/login.html");//同步读取文件optfile.readfile("D:nodejs/views/login.html",recall);//异步读取文件},regist:function(req,res){  //创建一个闭包,作为参数传过去recall=getRecall(req,res);// optfile.readfileSync("D:nodejs/views/login.html");//同步读取文件optfile.readfile("D:nodejs/views/regist.html",recall);//异步读取文件},//写文件writeFile:function(req,res){recall=getRecall(req,res);  optfile.writeFile("D:nodejs/views/write.text","写文件",recall);},readImage:function(req,res){recall=getRecall(req,res);  optfile.readImage("D:nodejs/images/one.jpg",recall);},showImage:function(req,res){      res.writeHead(200, {'Content-Type': 'image/jpeg'});    optfile.readImage("D:nodejs/images/one.jpg",res);}}

********************************optfile.js具体执行代码

var fs=require('fs');//引入文件操作模块module.exports={readfileSync:function(path){  var data=fs.readFileSync(path,'utf-8');//同步方法读取文件  console.log(data);  console.log('同步方法执行完毕');},readfile:function(path,recall){  fs.readFile(path,function(err,data){if(err){  console.log(err);}else {  console.log(data.toString());  recall(data);}  });  console.log("异步方法执行完毕");},//读取图片readImage:function(path,res){  fs.readFile(path,"binary",function(err,data){    if(err){      console.log(err);      return;    }else {    res.write(data,"binary");    res.end("");    }  });},//异步写文件writeFile:function (path,data,recall){  fs.writeFile(path,data,function(err){if(err){  throw err;}console.log("savesuccess");recall("写文件成功");  });}}

*******************************网页显示,表单提交;

登陆

用户名
密码

==========================POST提交,接收参数,=========

var querystring = require('querystring');

module.exports={login:function(req,res){//post方式接受参数var post='';//*************req.on('data',function(chunk){//***************  post +=chunk;//****************});//*******************req.on('end',function(){//req的data事件监听//*****************post=querystring.parse(post);//*********************console.log('收到参数email:'+post['email']);//**************});//***************************  //创建一个闭包,作为参数传过去recall=getRecall(req,res);// optfile.readfileSync("D:nodejs/views/login.html");//同步读取文件optfile.readfile("D:nodejs/views/login.html",recall);//异步读取文件}}

转载地址:http://hjmgi.baihongyu.com/

你可能感兴趣的文章
《程序员》:携程移动端 UI 界面性能优化实践
查看>>
Android指纹识别深入浅出分析到实战
查看>>
你们要的多数据库功能终于来了
查看>>
Android中实现微信本地视频发布到朋友圈功能
查看>>
非替代品,MongoDB与MySQL对比分析
查看>>
Hadoop平台相关技术
查看>>
java学习11天-自定义异常&异常转换(实例应用)
查看>>
MySql、SqlServer、Oracle数据库行转列大全
查看>>
程序员常用的自助建站资源汇总!
查看>>
分布式与集群的区别是什么?
查看>>
MySql常用必备脚本大全
查看>>
Velocity初探小结--velocity使用语法详解
查看>>
设计模式学习 - Singleton Pattern
查看>>
学习Spring——依赖注入
查看>>
CSS3 transform 属性详解
查看>>
Java对象内存结构及大小计算
查看>>
Spring MVC注解的一些案列
查看>>
Web大文件断点续传,快来看看吧!
查看>>
javascript高级编程3第二章:在html中使用javascript
查看>>
Android中热修复框架AndFix原理解析及案例使用
查看>>