---恢复内容开始---

后端中服务器类型有两种
1. web服务器【 静态服务器 】
- 举例: wamp里面www目录
- 目的是为了展示页面内容
- 前端: nginx
2. 应用级服务器[ api服务器 ]
- 后端接口
- tomcat
做什么?
- 使用Node.js原生代码实现静态服务器 【 必会 】
const http = require( 'http' )

const port = 3000

const hostname = 'localhost' // 127.0.0.1

http.createServer((request,response) => {

response.writeHead( 200, {
'Content-Type': 'text/html;charset=utf8' //如果输出内容含有中文,设置字符编码
}) response.write('hello Node.js') response.end() }).listen(port,hostname,() => {
// 参数: 端口 域名 监听回调
console.log(`The Server is running at: http://${ hostname }:${ port }`)
})

可以和爬虫结合使用,输出爬取的数据

可以和爬虫结合使用,输出爬取的数据
const http = require( 'http' ) const port = 3000 const hostname = 'localhost' // 127.0.0.1 const cheerio = require( 'cheerio' ) const options = {
hostname: 'jx.1000phone.net',
port: 80,
path: '/teacher.php/Class/classDetail/param/rqiWlsefmajGmqJhXXWhl3ZiZGZp',
method: 'GET',
headers: {
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
'Cache-Control':' no-cache',
Cookie: 'PHPSESSID=ST-22290-Uo8KnobsTgDO-TrQvhjA4TfoJI4-izm5ejd5j1npj2pjc7i3v4z',
Host: 'jx.1000phone.net',
Pragma: 'no-cache',
'Proxy-Connection': 'keep-alive',
Referer: 'http://jx.1000phone.net/teacher.php/Class/index',
'Upgrade-Insecure-Requests': 1,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': 0
}
}; http.createServer((request,response) => { response.writeHead( 200, {
'Content-Type': 'text/html;charset=utf8'
}) const req = http.get( options, (res) => {
const { statusCode } = res; // 获取状态码 1xx - 5xx
const contentType = res.headers['content-type']; // 文件类型 text/json/html/xml res.setEncoding('utf8'); // 字符编码 // 核心 -- start
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; }); // 数据拼接
res.on('end', () => { // 数据获取结束
try { const $ = cheerio.load( rawData ) $('td.student a').each( function ( item ) {
response.write( `<h3> ${ $( this ).text() } </h3>` )
})
response.end()
} catch (e) {
console.error(e.message);
}
}); // 核心 -- end
}).on('error', (e) => {
console.error(`Got error: ${e.message}`);
}); req.end() }).listen(port,hostname,() => {
// 参数: 端口 域名 监听回调
console.log(`The Server is running at: http://${ hostname }:${ port }`)
})

---恢复内容结束---

用Node.js原生代码实现静态服务器的更多相关文章

  1. 使用Node.js原生API写一个web服务器

    Node.js是JavaScript基础上发展起来的语言,所以前端开发者应该天生就会一点.一般我们会用它来做CLI工具或者Web服务器,做Web服务器也有很多成熟的框架,比如Express和Koa.但 ...

  2. Node.js原生及Express方法实现注册登录原理

    由于本文只是实现其原理,所以没有使用数据库,只是在js里面模拟数据库,当然是种中还是需要用数据库的. 1.node.js原生方法 ①html页面,非常简单,没有一丝美化~我们叫它user.html & ...

  3. js原生代码实现轮播图案例

    一.轮播图是现在网站网页上最常见的效果之一,对于轮播图的功能,要求不同,效果也不同! 我们见过很多通过不同的方式,实现这一效果,但是有很多比较麻烦,而且不容易理解,兼容性也不好. 在这里分享一下,用j ...

  4. node.js中net模块创建服务器和客户端(TCP)

    node.js中net模块创建服务器和客户端 1.node.js中net模块创建服务器(net.createServer) // 将net模块 引入进来 var net = require(" ...

  5. 仿jQuery的siblings效果的js原生代码

    仿jQuery的siblings效果的js原生代码 <previousSibling> 属性返回选定节点的上一个同级节点(在相同树层级中的前一个节点). <nextSibling&g ...

  6. 极简 Node.js 入门 - 5.3 静态资源服务器

    极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...

  7. 用node.js实现简单的web服务器

    node.js实现web服务器还是比较简单的,我了解node.js是从<node入门>开始的,如果你不了解node.js也可以看看! 我根据那书一步一步的练习完了,也的确大概了解了node ...

  8. node.js原生后台进阶(二)

    上一章讲到怎么样用原生node.js来获取GET.POST(urlencoded,formData)的参数,这一次我们更进一步,讲一下以下的点: 1.压缩(zlib) 2.流(stream) 3.路由 ...

  9. Node.js 教程 03 - 创建HTTP服务器

    前言: 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个"接收 HTTP 请 ...

随机推荐

  1. python-unittest生成报告的几种方式

    import unittest suite = unittest.TestSuite() #构造套件 #按测试方法添加 suite.addTest(测试类名('方法名')) suite.addTest ...

  2. React Native 中不用手点击,代码实现切换底部tabBar

    参考:https://www.jianshu.com/p/02c630ed7725 tabBar1页面有按钮,点击切换到tabBar2 直接用this.props.navigation.navigat ...

  3. SQL server 字段合并CAST(org_no AS VARCHAR(20))+CAST(page_no AS VARCHAR(20))+CAST(djlb_no AS VARCHAR(20)))

    sql server 字段合并(CAST) ---------------------- select (CAST(org_no AS VARCHAR(20))+CAST(page_no AS VAR ...

  4. formdata方式上传文件,支持大文件分割上传

    1.upload.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/html"> <h ...

  5. php命令行工具

    https://jingyan.baidu.com/article/37bce2beb6e5681002f3a20f.html

  6. matplotlib中中文字体配置

    解决方式1:利用matplotlib的字体管理工具font_manager---->缺点:每次必须要进行设置 import matplotlib.pyplot as plt from matpl ...

  7. border、outline、boxshadow那些事

    border 边框是我们美化网页.增强样式最常用的手段之一.例如: <div class="text"></div> .text { width: 254p ...

  8. 深入JavaWeb技术世界15:深入浅出Mybatis基本原理

    本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...

  9. swiper(轮播)组件

    swiper是一个非常强大的组件 但是需要swiper-item这个标签来实现他想显示的内容 swiper-item标签有个item-id的属性,属性值:字符串 是swiper-item的标识符: 一 ...

  10. PCIE手札

    PCIE兼容了大部分PCI总线的特性,区别在于使用串行差分总线代替了并行总线,并实现了协议分层.PCIE的带宽与LANE数量和时钟频率相关,时钟频率支持2.5G和5G,Lane支持x1/x2/x4/x ...