Node学习(二) --使用http和fs模块实现一个简单的服务器

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.对数据库中存储的数据进行读写操作。

原文地址:https://www.cnblogs.com/lijianming180/p/12258785.html