1 Star 0 Fork 0

CPC123456/nodeLocalService

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
server.js 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
chenpeichengc 提交于 2019-07-04 01:01 . feat:添加直播流videojs
var PORT = 3000;//
var wwwRootPath = "wwwroot";
var http = require('http');
var url = require('url');
var fs = require('fs');
var mine = require('./mine').types;//
var path = require('path');
var server = http.createServer(function (request, response) {
var pathname = url.parse(request.url).pathname;
if (pathname === "/") {
pathname = "index.html"
}
var realPath = path.join(wwwRootPath, pathname); //这里设置自己的文件名称;
console.log("访问文件",realPath);
var ext = path.extname(realPath);
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();
}
});
}
});
});
server.listen(PORT);
console.log("server ok");
console.log("请将需要的文件放入文件根目录:" + wwwRootPath);
console.log("访问的地址:http://localhost:"+PORT);
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
NodeJS
1
https://gitee.com/cpc12345/nodeLocalService.git
git@gitee.com:cpc12345/nodeLocalService.git
cpc12345
nodeLocalService
nodeLocalService
master

搜索帮助