创建web服务器
用node创建本地web服务
1,创建本地文件server.js
var http = require('http');
var url=require('url');
var fs=require('fs');
var mine=require('./mime').types;
var path=require('path'); //创建服务器
http.createServer(function(request, response) {
var pathname = url.parse(request.url).pathname;
var realPath = path.join("assets", pathname); var ext = path.extname(realPath);
if (ext == "") {
ext = ".html";
realPath += "index.html";
} ext = ext ? ext.slice(1) : 'unknown';
fs.exists(realPath, function (exists) {
if (!exists) {
response.writeHead(404, {
'Content-Type': 'text/plain'
}); response.write("This request URL " + pathname + " was not found on this server.");
response.end();
} else {
fs.readFile(realPath, "binary", function (err, file) {
if (err) {
response.writeHead(500, {
'Content-Type': 'text/plain'
});
response.end(err);
} else {
var contentType = mine[ext] || "text/plain";
response.writeHead(200, {
'Content-Type': contentType
});
response.write(file, "binary");
response.end();
}
});
}
});
}).listen(8080);
2,创建加载类型mime.js
exports.types = {
"css": "text/css",
"gif": "image/gif",
"html": "text/html",
"ico": "image/x-icon",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"pdf": "application/pdf",
"png": "image/png",
"svg": "image/svg+xml",
"swf": "application/x-shockwave-flash",
"tiff": "image/tiff",
"txt": "text/plain",
"wav": "audio/x-wav",
"wma": "audio/x-ms-wma",
"wmv": "video/x-ms-wmv",
"xml": "text/xml",
"mp3": "audio/mpeg",
"ogg": "audio/ogg",
"zip": "application/zip"
};
3,创建一个文件夹assets放资源
4 node server.js运行
创建web服务器的更多相关文章
- 使用express创建web服务器
使用express创建web服务器 laiqun@msn.cn Contents 1. 简单的express服务器 2. 静态文件服务 3. 路由 4. 中间件 1. 简单的express服务器 安装 ...
- 使用nodejs的http模块创建web服务器
使用nodejs的http模块创建web服务器 laiqun@msn.cn Contents 1. web服务器基础知识 2. Node.js的Web 服务器 3. 代码实现 1. web服务器基础知 ...
- 如何在Eclipse中创建Web服务器
使用Eclipse开发Web项目时,需要先配置Web服务器,如果已经配置好Web服务器,就不需要再重新配置了.也就是说,本节的内容不是开发每个项目时,都必须经过的步骤.创建Web服务器的具体步骤如下: ...
- 06-Node.js学习笔记-创建web服务器
创建web服务器 //引用系统模块 const http = require('http'); //创建web服务器 //用于处理url地址 const url = require('url'); c ...
- 初步认识express,并创建web服务器,挂载静态资源
1.Express简介 1.1什么是Express 官方给出的概念:Express 是基于 Node.js 平台,快速.开放.极简的 Web 开发框架,官方网址 相似用途:Express 的作用和 N ...
- NodeJs 创建 Web 服务器
以下是演示一个最基本的 HTTP 服务器架构(使用8081端口),创建 ser.js 文件,代码如下所示: var http = require('http'); var fs = require(' ...
- 十八、Node.js创建Web服务器(二)
在上一篇文章中我们在自定义模块自定义了几种文件类型的头文件加工的方法: /** * 自定义模块加工响应头文件类型 */ module.exports.getminiName=function (ext ...
- swoole 创建web服务器
http_server.php $http = new swoole_http_server("0.0.0.0", 9501); // 请求监听事件 $http->on('r ...
- node.js使用express模块创建web服务器应用
1.安装express模块 在命令行输入 npm install body-parser --save npm install express --save 2.创建app.js文件 /*应用程序入口 ...
随机推荐
- 微信小程序开发-踩坑
异步请求处理 详情描述: 微信小程序的wx.request({})请求时异步处理,以下代码 wx.reuest({ url:"https://XXXA", method:" ...
- Windows Server 2008 R2 服务器内存使用率过高几乎耗光
系统环境: Windows Server 2008 R2 Enterprise 搭建有 web服务器(iis) 和 文件服务 问题描述: Windows Server 2008 R2系统内存耗光 ...
- java课后实验性问题2
课后作业一:计算组合数 程序设计思想: 从键盘获取组合数,判断是否构成组合数.分别用三种方法计算组合数输出. 程序流程图: import java.util.Scanner; public class ...
- git常用操作方法
在实际开发,测试中,会使用git作为版本控制工具来完成团队协作.因此,对基本的git操作指令进行总结是十分有必要的,本文对一些术语或者理论基础,不重新码字,可以参考廖雪峰老师的博文,本文只对命令做归纳 ...
- 初中知识回顾tan,sin,cos关系
如果K=tan, sin 是X x=k/power(1+k*k,0.5) 开平方 cos是y y=1.0/power(1+k*k,0.5) 开平方 gisoracle总结 ============= ...
- 关于比较js中两个对象相等 ==
“如果两个操作数都是对象,则比较他们是不是同一个对象(引用的对象在内存中的地址一样),如果两个操作数都指向同一个对象,则相等操作符返回true,否则,返回false”. 我做了一个例子 functio ...
- MySQL数据库相关资料
python 全栈开发,Day60(MySQL的前戏,数据库概述,MySQL安装和基本管理,初识MySQL语句) python 全栈开发,Day61(库的操作,表的操作,数据类型,数据类型(2),完整 ...
- Flutter 底部的renderflex溢出
一开始直接使用Scaffold布局,body:new Column 然后模拟器会提示捕获异常: 然后百度了一下Flutter的溢出问题,发现解决办法是使用SingleChildScrollView包 ...
- 删除kubernetes-dashboard
kubectl get secret,sa,role,rolebinding,services,deployments --namespace=kube-system | grep dashboard ...
- C#实现排列、组合
排列组合的概念 排列:从n个不同元素中取出m(m≤n)个元素,按照一定的顺序排成一列,叫做从n个元素中取出m个元素的一个排列(Arrangement). 组合:从m个不同的元素中,任取n(n≤m)个元 ...