获取GET请求内容

node.js 中 url 模块中的 parse 函数提供了这个功能。

var http = require('http');
var url = require('url');
var util = require('util'); http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
res.end(util.inspect(url.parse(req.url, true)));
}).listen(3000);

获取 URL 的参数

var http = require('http');
var url = require('url');
var util = require('util'); http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain'}); // 解析 url 参数
var params = url.parse(req.url, true).query;
res.write("网站名:" + params.name);
res.write("\n");
res.write("网站 URL:" + params.url);
res.end(); }).listen(3000);

获取 POST 请求内容

var http = require('http');
var querystring = require('querystring'); var postHTML =
'<html><head><meta charset="utf-8"><title>菜鸟教程 Node.js 实例</title></head>' +
'<body>' +
'<form method="post">' +
'网站名: <input name="name"><br>' +
'网站 URL: <input name="url"><br>' +
'<input type="submit">' +
'</form>' +
'</body></html>'; http.createServer(function (req, res) {
var body = "";
req.on('data', function (chunk) {
body += chunk;
});
req.on('end', function () {
// 解析参数
body = querystring.parse(body);
// 设置响应头部信息及编码
res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'}); if(body.name && body.url) { // 输出提交的数据
res.write("网站名:" + body.name);
res.write("<br>");
res.write("网站 URL:" + body.url);
} else { // 输出表单
res.write(postHTML);
}
res.end();
});
}).listen(3000);

使用 Node 创建 Web 服务器(服务端创建服务器,解析请求,读取文件内容,发送响应数据(响应头部、响应内容))

创建server.js

var http = require('http');
var fs = require('fs');
var url = require('url'); // 创建服务器
http.createServer( function (request, response) {
// 解析请求,包括文件名
var pathname = url.parse(request.url).pathname; // 输出请求的文件名
console.log("Request for " + pathname + " received."); // 从文件系统中读取请求的文件内容
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
// HTTP 状态码: 404 : NOT FOUND
// Content Type: text/plain
response.writeHead(404, {'Content-Type': 'text/html'});
}else{
// HTTP 状态码: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/html'}); // 响应文件内容
response.write(data.toString());
}
// 发送响应数据
response.end();
});
}).listen(8081); // 控制台会输出以下信息
console.log('Server running at http://127.0.0.1:8081/');

创建一个 index.html

<html>
<head>
<title>Sample Page</title>
</head>
<body>
Hello World!
</body>
</html>

进入http://127.0.0.1:8081/index.html

使用 Node 创建 Web 客户端(向服务端请求,请求的选项,不断更新数据,数据接收完成)

var http = require('http');

// 用于请求的选项
var options = {
host: 'localhost',
port: '8081',
path: '/index.htm'
}; // 处理响应的回调函数
var callback = function(response){
// 不断更新数据
var body = '';
response.on('data', function(data) {
body += data;
}); response.on('end', function() {
// 数据接收完成
console.log(body);
});
}
// 向服务端发送请求
var req = http.request(options, callback);
req.end();

运行后得到结果

【node.js】GET/POST请求、Web 模块的更多相关文章

  1. node.js(二)各种模块

    我们知道Node.js适合于IO密集型应用,不适合于CPU密集型应用.    JS和Node.js区别:         JS运行于客户端浏览器中,存在兼容性问题:数据类型:值类型+引用类型(ES+D ...

  2. Node.js GET/POST请求

    在很多场景中,我们的服务器都需要跟用户的浏览器打交道,如表单提交. 表单提交到服务器一般都使用GET/POST请求. 我将为大家介绍 Node.js GET/POST请求. 获取GET请求内容 由于G ...

  3. node.js 发送http 请求

    自己研究了一下 node.js 的 http模块  下面为想服务器发送请求的代码 ,通过学习了解http 请求的过程 ,node.js 对http请求的原始封装比较低,以前php 可以用$_GET , ...

  4. [Node.js与数据库]node-mysql 模块介绍

    [Node.js与数据库]node-mysql 模块介绍   转载至:https://itbilu.com/nodejs/npm/NyPG8LhlW.html#multiple-statement-q ...

  5. body-parser Node.js(Express) HTTP请求体解析中间件

    body-parser Node.js(Express) HTTP请求体解析中间件 2016年06月08日     781     声明 在HTTP请求中,POST.PUT和PATCH三种请求方法中包 ...

  6. Node.js进程管理之Process模块

    在前面Node.js事件运行机制也有提到,Node.js应用在单个线程运行,但是现在大部分服务器都是多处理器,为了方便使用多个进程,Node.js提供了3个模块.Process模块提供了访问正在运行的 ...

  7. Node.js 从零开发 web server博客项目[express重构博客项目]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  8. Node.js 从零开发 web server博客项目[登录]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  9. Node.js 从零开发 web server博客项目[项目介绍]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  10. Fenix – 基于 Node.js 的桌面静态 Web 服务器

    Fenix 是一个提供给开发人员使用的简单的桌面静态 Web 服务器,基于 Node.js 开发.您可以同时在上面运行任意数量的项目,特别适合前端开发人员使用. 您可以通过免费的 Node.js 控制 ...

随机推荐

  1. finally 的作用是什么?

    在java中finally首先必须使用在所有catch的最后位置, 无论是否抛出异常,finally代码块总是会被执行.就算是没有catch语句同时又抛出异常的情况下,finally代码块任然会被执行 ...

  2. 悟空模式-java-抽象工厂模式

    [一朝,王母娘娘设宴,大开宝阁,瑶池中做蟠桃胜会] 有一天,王母娘娘要在瑶池办party,就需要准备大量的食材.要知道,天上的神仙也分三六九等,九曜星.五方将.二十八宿.四大天王.十二元辰.五方五老. ...

  3. Spring 中面向AOP之一系列做法

    Spring的AOP实现是通过集成AspectJ框架实现的. 想必这句话大家都知道了吧,不知道也没关系,最起码你现在知道了. 四种实现方案,接下来我们一一去揭开它的神秘的面纱....... 第一种(伪 ...

  4. Vue项目打包报错Failed to load resource: net::ERR_FILE_NOT_FOUND

    webpack.prod.conf.js 中output添加参数publicPath:'./' 修改webpack.base.conf.js中: publicPath: process.env.NOD ...

  5. 将ArcGIS Server的JSON转化为SHP文件

    # -*- coding: utf-8 -*- # -------------------------------------------------------------------------- ...

  6. oracle 用户系统权限

    conn sys as sysdba; create user test identified by test; grant create session to test; grant create ...

  7. shell黑名单

    #/bin/bash netstat -ant | " | awk '{print $5}' | grep -v "^10" | cut -d ":" ...

  8. python函数 变量 递归

    1 语法 #语法 def 函数名(参数1,参数2,参数3,...): '''注释''' 函数体 return 返回的值 #函数名要能反映其意义 返回值数=0:返回None放回值数=1:返回object ...

  9. leetcode 之 Same Tree

    1.题目描述 Given two binary trees, write a function to check if they are the same or not. Two binary tre ...

  10. 关于Oracle中sys、system和Scott用户下的数据库连接问题

    system默认:manager sys默认:change_on_install 使用SQL Plus登录数据库时,system使用密码manager可直接登录. 由于为自己的密码时更改过的,所以我的 ...