案例通过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");
});

  

CSS   详细X
基本翻译
abbr. 半铸钢,钢性铸铁(Cast Semi-Steel);层叠样式表(Cascading Style Sheets);计算机系统模拟(Computer System Simulation)
网络释义
CSS: 层叠样式表
CSS Sprites: 图片合并
CSS Syntax: 样式表语法

nodejs基础-HTTP的更多相关文章

  1. [转]Nodejs基础中间件Connect

    Nodejs基础中间件Connect 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的J ...

  2. Nodejs基础中间件

    Nodejs基础中间件Connect   http://www.tuicool.com/articles/emeuie 关于作者 张丹(Conan), 程序员Java,R,PHP,Javascript ...

  3. Nodejs第一天-{Nodejs基础 深刻理解浏览器 环境变量 基础语法}

    Nodejs第一天 1.什么是Nodejs ​ Nodejs是一个可以运行(解析)ECMAScript的环境; ​ ECMAScript是规定了一些列的语法 ,这些语法想要解析的执行就需要放在某个环境 ...

  4. nodejs 基础篇整合

    nodeJs 基础篇整合 最近有朋友也想学习nodeJs相关方面的知识,如果你是后端想接近前端,node作为一门跑在服务端的JS语言从这里入门再好不过了.如果你正好喜欢前端,想走的更高,走的更远.no ...

  5. 前端知识体系-NodeJS相关】NodeJS基础知识全面总结

    NodeJS基础知识 1. Node的全局对象和全局变量 1.1 全局对象:所有模块都可以调用的 global:表示Node所在的全局环境,类似于浏览器的window对象. process:该对象表示 ...

  6. Nodejs基础中间件Connect

    http://www.tuicool.com/articles/emeuie 关于作者 张丹(Conan), 程序员Java,R,PHP,Javascript weibo:@Conan_Z blog: ...

  7. 02 nodejs命令参数(NodeJS基础入门)

    声明:本文章可供有一定js基础的朋友参考nodejs入门,本文未讲解nodejs的安装,如有需要的同学可以加QQ3382260752找我,进行交流学习. 建议使用开发软件:webstorm或hbuil ...

  8. nodejs基础教程回顾01

    最近在复习nodejs,因为框架太多隔一段时间不用就会忘了,所以没办法必须时常拿出来练练,就像家里有好几辆车,要时不常的轮流开一圈.我就从最基础的开始写,怎么下载安装就不说了,首先是nodejs的三类 ...

  9. NodeJS基础总结(一)

    NodeJS官网网址:https://nodejs.org/en/ 使用require方法加载fs核心模块 var fs = require('fs'); 一.读取文件//   第一个参数就是尧读取的 ...

  10. NodeJS基础教程

    关于 本书致力于教会你如何用Node.js来开发应用,过程中会传授你所有所需的“高级”JavaScript知识.本书绝不是一本“Hello World”的教程. 状态 你正在阅读的已经是本书的最终版. ...

随机推荐

  1. sys模块与shutil模块

    #coding=utf-8 import sys ## sys.argv #从命令行获取参数 import shutil #文件.文件夹.压缩包.处理模块 f1 = open("test.t ...

  2. 前端开发HTML&css入门——常用的标签以及一个小练习

    meta标签 <!doctype html> <html> <head> <meta charset="utf-8" /> < ...

  3. Spark集成Kafka实时流计算Java案例

    package com.test; import java.util.*; import org.apache.spark.SparkConf; import org.apache.spark.Tas ...

  4. Django 路由层与视图层

    1.路由层 1.1无名分组 1.2 有名分组 1.3 反向解析 1.4 路由分发 1.5 名称空间 2.伪静态网页 3.虚拟环境 4.视图层 1.1 JsonResponse 1.2 FBV与CBV ...

  5. 2018-8-10-使用-Resharper-特性

    title author date CreateTime categories 使用 Resharper 特性 lindexi 2018-08-10 19:16:51 +0800 2018-4-25 ...

  6. 微信支付MD5签名算法C#版,ASCII码字典序排序0,A,B,a,b

    /// <summary> /// 微信支付MD5签名算法,ASCII码字典序排序0,A,B,a,b /// </summary> /// <param name=&qu ...

  7. linux里面以指定用户运行命令

    一.chroot方式 [root@localhost ~]# chroot --userspec "nginx:nginx" "/" sh -c "w ...

  8. bzoj4448 [Scoi2015]情报传递 主席树+树上差分

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4448 题解 练习一下主席树的基础练习题找回感觉. 对于每一次询问,第一问显然随便做. 第二问的 ...

  9. bzoj4811 [Ynoi2017]由乃的OJ 树链剖分+贪心+二进制

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4811 题解 我现在为什么都写一题,调一天啊,马上真的退役不花一分钱了. 考虑这道题的弱化版 N ...

  10. redis学习 --Hash

    一:我们可以将Redis中的Hash类型看成具有String Key和String Value的map容器.所以该类型非常适合于存储值对象的信息.如Username.Password和Age等.如果H ...