您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 贺州分类信息网,免费分类信息发布

在Node.js中如何创建Web、TCP服务器

2025/1/23 1:19:51发布20次查看
这篇文章主要介绍了用node.js创建web服务器和tcp服务器的方法和处理技巧,需要的读者们学习一下吧。
使用http模块创建web服务器
web服务器的功能:
接受http请求(get、post、delete、put、patch)
处理http请求(自己处理,或请求别的程序处理)
做出响应(返回页面、文件、各类数据等)
常见的web服务器架构:
nginx、apache:负责接受http请求,确定谁来处理请求,并返回请求的结果
php-fpm / php模块:处理分配给自己的请求,并将处理结果返回给分配者
常见请求种类:
请求文件:包括静态文件(网页、图片、前端javascript文件、css文件...),及由程序处理得到的文件
完成特定的操作:如登录、获取特定数据等
node.js的web服务器:
不依赖其他特定的web服务器软件(如apache、nginx、iis......)
node.js代码处理请求的逻辑
node.js代码负责web服务器的各种“配置”
使用express创建web服务器
简单的express服务器
静态文件服务
路由
中间件
简单的express服务器:
var express = require('express'); var app = express(); app.get('', function(req, res){ <span style="white-space:pre"> </span>res.end('hello\n'); <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>app.listen(18001, function afterlisten(){ <span style="white-space:pre"> </span>console.log('express running on http://localhost:18001'); <span style="white-space:pre"> </span>});
静态文件范围:
网页、纯文本、图片、前端javascript代码、css样式表文件、媒体文件、字体文件
使用express访问静态文件
<span style="white-space:pre"></span>app.use(express.static('./public'));
路由:
将不同的请求,分配给相应的处理函数
区分:路径、请求方法
三种路由实现方法:
path:比较简单
router:比较适合同一个路由下的多个子路由
route:比较适合api
中间件
connect:node.js的中间件框架
分层处理
每层实现一个功能
创建tcp服务器
使用net模块创建tcp服务器
使用telnet连接tcp服务器
使用net创建tcp客户端
利用node.js搭建简单web服务器js代码部分:
var http = require('http');var url = require('url');var path = require('path');var fs = require('fs');var dir, arg = process.argv[2] || ''; // 命令行第三个参数,用来接收目录,可为空,相对当前server.js文件的目录名称// 比如使用命令 node server debug,意思就是debug文件夹与server.js文件同级// 且你想以debug文件夹启动web服务 http.createserver(function (req, res) {var pathname = __dirname + url.parse(req.url).pathname;dir = dir ? dir : pathname; // 记住dir(目录)pathname = dir ? pathname.replace(dir, dir + arg + '/') : pathname; // 替换文件静态路径if (path.extname(pathname) == "") {pathname += "/";}if (pathname.charat(pathname.length - 1) == "/") {pathname += "index.html"; // 入口文件,此处默认index.html} fs.exists(pathname, function (exists) {if (exists) {switch (path.extname(pathname)) {case ".html":res.writehead(200, {"content-type": "text/html"});break;case ".js":res.writehead(200, {"content-type": "text/javascript"});break;case ".css":res.writehead(200, {"content-type": "text/css"});break;case ".gif":res.writehead(200, {"content-type": "image/gif"});break;case ".jpg":res.writehead(200, {"content-type": "image/jpeg"});break;case ".png":res.writehead(200, {"content-type": "image/png"});break;default:res.writehead(200, {"content-type": "application/octet-stream"});} // res可以自己添加信息来简单交互 比如可以修改点header信息 或者修改返回的资源数据fs.readfile(pathname, function (err, data) {res.end(data);});}else {res.writehead(404, {"content-type": "text/html"});res.end("<h1>404 not found</h1>");}});}).listen(8085, "127.0.0.5"); // 服务器端口console.log("server running at http://127.0.0.5:8085/");
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
在laydate.js中加载路径出现错误
在vue-router中如何实现路由传参
使用jquery操作table如何实现单元格合并
以上就是在node.js中如何创建web、tcp服务器的详细内容。
贺州分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录