欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

从零开始学习Node.js系列教程二:文本提交与显示方法

程序员文章站 2022-04-28 09:21:10
本文实例讲述了node.js文本提交与显示方法。分享给大家供大家参考,具体如下: index.js var server = require("./server...

本文实例讲述了node.js文本提交与显示方法。分享给大家供大家参考,具体如下:

index.js

var server = require("./server");
var router = require("./router");
var requesthandlers = require("./requesthandlers");
var handle = {}
handle["/"] = requesthandlers.start;
handle["/start"] = requesthandlers.start;
handle["/upload"] = requesthandlers.upload;
server.start(router.route, handle);

server.js

var http = require("http");
var url = require("url");
function start(route, handle) {
 function onrequest(request, response) {
  var postdata = "";
  var pathname = url.parse(request.url).pathname;
  console.log("request for " + pathname + " received.");
  request.setencoding("utf8");
  request.addlistener("data", function(postdatachunk) {
   postdata += postdatachunk;
   console.log("received post data chunk '"+
   postdatachunk + "'.");
  });
  request.addlistener("end", function() {
   console.log("data received ending" + pathname);
   route(handle, pathname, response, postdata);
  });
 }
 http.createserver(onrequest).listen(8888);
 console.log("server has started.");
}
exports.start = start;

requesthandlers.js

var querystring = require("querystring");
function start(response, postdata) {
 console.log("request handler 'start' was called.");
 var body = '<html>'+
  '<head>'+
  '<meta http-equiv="content-type" content="text/html; '+
  'charset=utf-8" />'+
  '</head>'+
  '<body>'+
  '<form action="/upload" method="post">'+
  '<textarea name="text" rows="20" cols="60"></textarea>'+
  '<input type="submit" value="submit text" />'+
  '</form>'+
  '</body>'+
  '</html>';
  response.writehead(200, {"content-type": "text/html"});
  response.write(body);
  response.end();
}
function upload(response, postdata) {
 console.log("request handler 'upload' was called.");
 response.writehead(200, {"content-type": "text/plain"});
 response.write("you've sent the text: "+
 querystring.parse(postdata).text);
 response.end();
}
exports.start = start;
exports.upload = upload;

router.js

function route(handle, pathname, response, postdata) {
 console.log("about to route a request for " + pathname);
 if (typeof handle[pathname] === 'function') {
  handle[pathname](response, postdata);
 } else {
  console.log("no request handler found for " + pathname);
  response.writehead(404, {"content-type": "text/plain"});
  response.write("404 not found");
  response.end();
 }
}
exports.route = route;

result:

从零开始学习Node.js系列教程二:文本提交与显示方法从零开始学习Node.js系列教程二:文本提交与显示方法从零开始学习Node.js系列教程二:文本提交与显示方法从零开始学习Node.js系列教程二:文本提交与显示方法从零开始学习Node.js系列教程二:文本提交与显示方法

知识点:

require和exports的用法:

index.js中代码

var hello = require('.hello');
hello = new hello();
hello.setname('joey');
hello.sayhello();

hello.js中代码

function hello(){
  var name;
  this.setname = function(thyname){
    name = thyname;
  }
  this.sayhello = function(){
    console.log('hello ' + name);
  }
}
//exports.hello = hello; //此时我们在其他文件中需要通过 require('./hello').hello来获取hello对象,这种写法有点冗余
module.exports = hello; //输出的就是hello对象本身,不是上面的exports,上面的是暴露.hello,.hello赋予了hello对象

希望本文所述对大家nodejs程序设计有所帮助。