我是靠谱客的博主 糟糕水池,这篇文章主要介绍nodejs创建web服务-静态资源请求-过滤ico图片请求 服务器端资源路径node-web服务创建 ,现在分享给大家,希望可以做个参考。

 服务器端资源路径

node-web服务创建 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//引入模块 const http = require("http"); const urlObj = require("url"); const pathObj = require("path"); const fs = require("fs"); //创建web页面服务 const server = http.createServer((req, res) => { //过滤ico图片请求 if (req.url === "/favicon.ico") { res.end(); } else { //获取请求路径 let { pathname } = urlObj.parse(req.url, true);///www/index.html //判断 是否是根目录 if (pathname === "/") { //当访问根 默认访问index.html pathname = "/index.html"; } //获取扩展名 let { ext } = pathObj.parse(pathname); //设置响应头信息 res.writeHead(200, { "Content-type": getMine(ext)+";charset=UTF-8"}); console.log(pathname,ext,getMine(ext)); //响应相应文件 res.end(fs.readFileSync("." + pathname)); } }); function getMine(ext) { switch (ext) { case ".html": return "text/html"; case ".css": return "text/css"; case ".js": return "text/html"; case ".jpg": return "image/jpeg"; case ".jpeg": return "image/jpeg"; case ".json": return "application/json"; default: return "text/plan"; } } //设置端口 server.listen(3000);

后台服务打印信息 

请求地址                         文件扩展名             文件类型

/www/index.html                 .html                    text/html
/www/css/index.css            .css                      text/css
/www/img/01.jpg                 .jpg                     image/jpeg
/www/js/index.js                  .js                         text/html

最后

以上就是糟糕水池最近收集整理的关于nodejs创建web服务-静态资源请求-过滤ico图片请求 服务器端资源路径node-web服务创建 的全部内容,更多相关nodejs创建web服务-静态资源请求-过滤ico图片请求 服务器端资源路径node-web服务创建 内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部