nodejs基础-HTTP
案例通过nodejs编写http服务程序
步骤:
1,加载http模块
2.创建http服务
3、为http服务对象添加request事件处理程序
4·开启http服务监听,准备接收客户端请求
注意:
1,浏览器显示可能是乱码,所以可以通过.res.setHeader("Content-Type", 'text/plain; charset=utf-8');设置浏览器显示时所使用的编码.
2. Chrome浏览器默认无法手动设置编码,需要安装"Set Character Encoding"扩展.
3,演示一下设置Content-Type-text/html'和"Content-Type-text/plain的区别.

解决浏览器乱码,通过响应报文,指定浏览器解析编码格式
res.setHeader("Content-type","text/plain;charset=utf-8");
res.setHeader("Content-type","text/html;charset=utf-8");//解析html标签
根据不同的访问,请求不同的页面
通过node.js编写http服务程序,通过读取静态html文件来响应用户请求(带图片和外部css样式)
mime通过url的后缀返回输出头文件输出类型
模拟apache发布
var http = require("http");
var server = http.createServer();
var fs = require("fs");
var path = require("path");
var mime = require("mime");
server.on("request",function (req,res) {
// bod
res.write("hello wang 开发工程师");res.setHeader("Content-type","text/plain;charset=utf-8");
res.end();
});
server.listen(8080,function () {
// body...
console.log("服务器启动,请访问:http://localhost:8080");
});
//根据请求url返回不同的数据
http.createServer(function (req,res) {
// body...
console.log(req.url);
if (req.url === '/' || req.url === '/index') {
res.end('Hello index');
} else if(req.url === '/login'){
res.end('Hello login');
}else if(req.url === '/list'){
res.end('Hello list');
}else if(req.url === '/regiest'){
res.end('Hello regiest');
}else {
res.end('404 not found!');
}
}).listen(8888,function () {
// body...
console.log("http://localhost:8888");
})
//根据请求url返回不同的html
http.createServer(function (req,res) {
// body...
if (req.url === '/' || req.url === '/index') {
fs.readFile(path.join(__dirname,"index.html"),function (err, data) {
// body...
if (err) {
throw err;
}
res.end(data);
});
} else if(req.url === '/login'){
res.end('Hello login');
}else if(req.url === '/list'){
res.end('Hello list');
}else if(req.url === '/regiest'){
res.end('Hello regiest');
}else {
res.end('404 not found!');
}
}).listen("8090",function () {
// body...
console.log("监听请求!");
});
//try无法捕捉异步异常
var fs1 = require("fs");
try{
fs1.writeFile("./yyy/abd.txt","大家早上好","utf-8",function (err,data) {
// body...
if (err) {
console.log("出错了throw!");
}
console.log("ok");
});
}catch(e){
console.log("出错了catch");
}
//请求图片请求css文件
http.createServer(function (req,res) {
// body...
if (req.url === '/' || req.url === '/index') {
fs.readFile(path.join(__dirname,"index.html"),function (err, data) {
// body...
if (err) {
throw err;
}
res.end(data);
});
} else if(req.url === '/wang.png'){
fs.readFile(path.join(__dirname,"wang.png"),function (err,data) {
// body...
console.log("图片信息");
if (err) {
throw err;
}
res.setHeader("Content-type","image/png");
res.end(data);
});
}else if(req.url === '/body.css'){
fs.readFile(path.join(__dirname,"body.css"),function (err,data) {
// body...
console.log("样式信息");
if (err) {
throw err;
}
res.setHeader("Content-type","text/css");
res.end(data);
});
}else if(req.url === '/regiest'){
res.end('Hello regiest');
}else {
console.log("error404");
res.end('404 not found!');
}
}).listen("9090",function () {
// body...
console.log("监听请求9090!");
});
//模拟apache
http.createServer(function (req,res) {
// body...
var filename = path.join(__dirname,"public",req.url);
console.log("filename"+filename);
fs.readFile(filename,function(err,data) {
// body...
if (err) {
console.log(err);
console.log("出错了!");
}
res.setHeader("Content-type", mime.getType(filename));
res.end(data);
console.log("好了!");
});
}).listen("8899",function () {
// body...
console.log("监听请求8899!");
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index页面</title>
<link rel="stylesheet" type="text/css" href="./body.css">
<link rel="stylesheet" type="text/css" href="./a.css">
</head>
<body>
<h1 class="ttt">index页面</h1> <img src="./wang.png" alt="我的图片">
</body>
</html>
.
h1{
color : red;
}
在线请求服务器的时候,请求的url就是一个标识
request和response对象介绍
request:服务器解析用户提交的http请求报文,将结果解析到request对象中,凡是要获取和用户请求相关的数据都可以通过 request对象获取
response:在服务器端用来向用户做出响应的对象,凡是需要向用户(客户端)响应的操作,部需要通过[response对象来进行
request对象类型<http.IncomingMessage>,继承自stream. Readable
request对象常用成员
request.headers
request. rawHeadens
request.httpVersion
request.method
request.url
var http = require("http");
http.createServer(function (req,res) {
// body...
console.log(req.headers);
console.log(req.rawHeaders);
console.log(req.httpVersion);
console.log(req.method);
console.log(req.url);
res.end('Hello regiest');
}).listen("8080",function () {
// body...
console.log("http.request");
});


nodejs基础-HTTP的更多相关文章
- [转]Nodejs基础中间件Connect
Nodejs基础中间件Connect 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的J ...
- Nodejs基础中间件
Nodejs基础中间件Connect http://www.tuicool.com/articles/emeuie 关于作者 张丹(Conan), 程序员Java,R,PHP,Javascript ...
- Nodejs第一天-{Nodejs基础 深刻理解浏览器 环境变量 基础语法}
Nodejs第一天 1.什么是Nodejs Nodejs是一个可以运行(解析)ECMAScript的环境; ECMAScript是规定了一些列的语法 ,这些语法想要解析的执行就需要放在某个环境 ...
- nodejs 基础篇整合
nodeJs 基础篇整合 最近有朋友也想学习nodeJs相关方面的知识,如果你是后端想接近前端,node作为一门跑在服务端的JS语言从这里入门再好不过了.如果你正好喜欢前端,想走的更高,走的更远.no ...
- 前端知识体系-NodeJS相关】NodeJS基础知识全面总结
NodeJS基础知识 1. Node的全局对象和全局变量 1.1 全局对象:所有模块都可以调用的 global:表示Node所在的全局环境,类似于浏览器的window对象. process:该对象表示 ...
- Nodejs基础中间件Connect
http://www.tuicool.com/articles/emeuie 关于作者 张丹(Conan), 程序员Java,R,PHP,Javascript weibo:@Conan_Z blog: ...
- 02 nodejs命令参数(NodeJS基础入门)
声明:本文章可供有一定js基础的朋友参考nodejs入门,本文未讲解nodejs的安装,如有需要的同学可以加QQ3382260752找我,进行交流学习. 建议使用开发软件:webstorm或hbuil ...
- nodejs基础教程回顾01
最近在复习nodejs,因为框架太多隔一段时间不用就会忘了,所以没办法必须时常拿出来练练,就像家里有好几辆车,要时不常的轮流开一圈.我就从最基础的开始写,怎么下载安装就不说了,首先是nodejs的三类 ...
- NodeJS基础总结(一)
NodeJS官网网址:https://nodejs.org/en/ 使用require方法加载fs核心模块 var fs = require('fs'); 一.读取文件// 第一个参数就是尧读取的 ...
- NodeJS基础教程
关于 本书致力于教会你如何用Node.js来开发应用,过程中会传授你所有所需的“高级”JavaScript知识.本书绝不是一本“Hello World”的教程. 状态 你正在阅读的已经是本书的最终版. ...
随机推荐
- CentOS6.4下Mysql数据库的安装与配置,导入数据库,授权远程ip
卸载掉原有mysql 由于mysql数据库在Linux上实在是太流行了,所以眼下下载的主流Linux系统版本号基本上都集成了mysql数据库在里面,我们能够通过例如以下命令来查看我们的操作系统上是否已 ...
- k3 cloud列表中出现很多空白
解决办法:找到单据体:过滤面板默认隐藏打勾
- 关于conda-新手必读
一.管理conda 通过anaconda来安装python及python包,让你不必关心系统是否安装了一些依赖,如zlib等等,anaconda已经集成了这些依赖,可以方便的安装python 下载请点 ...
- c# 匿名委托
using System; namespace AnonymousMethod { delegate void ArithmeticOperation(double operand1, double ...
- MFC消息详解 (WindowProc|OnCommand|OnNotify)
1. 怎样使用MFC发送一个消息用MFC发送一个消息的方法是, 首先,应获取接收消息的CWnd类对象的指针: 然后,调用CWnd的成员函数SendMessage( ). LRESULT Res=pWn ...
- 解析 Java 反射题中一个有趣的坑
public class Test { public void age(int age) { System.out.println("int age="+age); } publi ...
- 域名Whois数据和隐私是最大风险
在互联网安全大会上,东方联盟掌门人,东方联盟郭盛华呼吁RIR机构应为全球协调分配地址上作出改进,实现whois数据准确性的前进方向.他还说,域名whois隐私信息应默认对外关闭,同时最近APNIC合作 ...
- 项目部署到tomcat,浏览器能够访问,手机不能访问。
问题:有这样一个问题,把项目部署到tomcat上,浏览器能够访问,但是手机不能访问. 解决:在 tomcat中找到conf文件夹,然后找到web.xml
- python全栈开发,Day41(线程概念,线程的特点,进程和线程的关系,线程和python理论知识,线程的创建)
昨日内容回顾 队列 队列:先进先出.数据进程安全 队列实现方式:管道+锁 生产者消费者模型:解决数据供需不平衡 管道 双向通信,数据进程不安全 EOFError: 管道是由操作系统进行引用计数的 必须 ...
- ctrl+鼠标左键监听
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...