1.创建一个www目录,存储静态文件1.html、1.jpg。

* html文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
网页内容
<img src="/1.jpg" alt="">
</body>
</html>
  1. 预期实现的结果为:
    a. 在浏览器访问http://localhost:8080/1.html
    b. 读取到www/1.html,由HTML文件发起对www/1.jpg的请求。
    c. 网页中显示HTML内容和图片。

  2. 使用Nodejs实现服务端代码:

1
大专栏  Node学习(二) --使用http和fs模块实现一个简单的服务器n class="line">2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const http = require('http')
const fs = require('fs') const server = http.createServer((request, response) => {
console.log(request.url)
fs.readFile(`./www${request.url}`, (error, buffer) => { // 根据URL查找读取相应的文件。
if (error) { // 若读取错误,则向前端返回404状态码,以及内容Not Found。
response.writeHead(404)
response.write('Not Found')
} else { // 若读取成功,则向前端返回读取到的文件。
response.write(buffer)
}
response.end() // 关闭连接。
})
}) server.listen(8080)

服务器需要具备的基本功能

1.响应请求
如上面的例子,可以根据客户端的请求做出回应,如返回静态文件。
2 数据交互
定义接口,客户端根据接口,与服务端进行数据交互。
例如在一个购物流程中,客户端向服务端请求商品数据,展现给客户,客户在购买时,客户端将购买的商品信息发送给服务端处理。
数据库
3.对数据库中存储的数据进行读写操作。

最新文章

  1. Source Insight 3.X utf8支持插件更新
  2. ios推送:本地通知UILocalNotification
  3. DataSanp的控制老大-DSServer
  4. 转:JAVA中this用法小结
  5. java学习——集合框架(泛型,Map)
  6. charset
  7. 如何成为一个优秀的DBA
  8. 数据采集工具Telegraf:简介及安装
  9. Mysql概念及基本操作
  10. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165321
  11. CodeForces - 1051D-简单DP
  12. MySQL5.7 编译安装
  13. kubectl命令自动补全
  14. C#-1-1-.net
  15. 几种outofmemory
  16. [转]Apache的CRT格式SSL证书转换成IIS用的PFX格式
  17. vdbench 数据校验测试方法
  18. eclipse 打开的时候弹出 &#39;Building workspace&#39; has encountered a problem. Errors occurred during
  19. The import java.util cannot be resolved
  20. 【起航计划 024】2015 起航计划 Android APIDemo的魔鬼步伐 23 App-&gt;Notification-&gt;IncomingMessage 状态栏通知

热门文章

  1. set theory
  2. TPO1-2 The Origin of Theater
  3. 传统方式接口测试返回值json验证
  4. iOS分段选择器、旅行App、标度尺、对对碰小游戏、自定义相册等源码
  5. MDS算法及其matlab实现
  6. spring boot GlobalExceptionHandler @RestControllerAdvice @ExceptionHandler
  7. 吴裕雄--天生自然C语言开发:函数
  8. logback日志大量写磁盘导致微服务不能正常响应的解决方案
  9. hibernate中lazy的使用
  10. zabbix 使用邮件发送告警信息