关于别的电脑访问node.js建的服务器
来源:互联网 发布:淘宝网商城广场舞服装 编辑:程序博客网 时间:2024/05/01 16:18
1、你得首先安装node.js,官网地址https://nodejs.org/下载node.exe执行文件,傻瓜式按装
2、按装完后,在node的安装位置
3、红色地方建三个文件,test.js,mine.js,Tools.js
打开test.js 并把下面这段代码复制进去
var PORT = 8888;var http = require('http');var url=require('url');var fs=require('fs');var mine=require('./mine').types;var path=require('path');var Tools=require('./Tools');//var tools = new Tools();var server = http.createServer(function (request, response) { var pathname = url.parse(request.url).pathname; var guessPage = Tools.guessPage(fs, path, path.dirname(process.execPath), pathname); var realPath = guessPage.realPath; var ext = guessPage.ext; fs.exists(realPath, function (exists) { if (!exists) { response.writeHead(404, { 'Content-Type': 'text/plain' }); response.write("This request URL " + realPath + " 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"; console.log("contentType: " + contentType); response.writeHead(200, { 'Content-Type': contentType }); response.write(file, "binary"); response.end(); } }); } });});server.listen(PORT);console.log("Server runing at port: " + PORT + ".");
打开 mine.js并把下面这段代码复制进去
exports.types = { "css": "text/css", "gif": "image/gif", "htm": "text/html", "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"};
打开 Tools.js并把下面这段代码复制进去
// if export Object, use constructor define//function Tools() {};// export Object//module.exports = Tools;/** * 补充请求页面,自动添加index.html/index.htm *///Tools.guessPageexports.guessPage = function(fs, path, curDir, pathname) { console.log("pathname: " + pathname); if (!pathname) { pathname = pathname+"/"; } var realPath = path.join(curDir, pathname); console.log("realPath: " + realPath); var ext = path.extname(realPath); console.log("before ext: " + ext); if (!ext) { // guess index.html, is not exist, then index.htm. var tmpPath = realPath + "index.htm"; console.log("tmpPath: " + tmpPath); // fs.existsSync will be deprecated. // var exists = fs.existsSync(tmpPath); var exists = true; try { fs.openSync(tmpPath, "r"); } catch (e) { console.log("e: " + e); exists = false; } console.log("exists: " + exists); if (exists) { ext = "htm"; } else { tmpPath = realPath + "index.html"; ext = "html"; } realPath = tmpPath; } else { ext = ext.slice(1); } console.log("ext: "+ext); var result = new Object(); result.realPath = realPath; result.ext = ext; return result;}
阅读全文
0 0
- 关于别的电脑访问node.js建的服务器
- 能访问别的电脑,但别的电脑ping不通服务器(转摘文章)
- 某些电脑访问不了别的主机的MYSQL数据库解决办法
- 手机成功访问node.js搭建的本地服务器
- 关于访问自己电脑做服务器的相关IP知识
- 关于android真机访问本地电脑服务器以及访问数据库的那点事
- node.js 搭载的一个服务器,局域网内,都可以访问
- node.js使用(五):网站访问、搭建服务器及留言功能的实现
- 关于node.js的误会
- 关于node.js的误会
- 手机访问 电脑虚拟机的服务器
- 手机访问电脑上的服务器
- 手机访问电脑上的服务器
- 微信小程序访问node.js接口服务器搭建教程
- 访问别的应用的SharedPreference
- 让其他电脑访问我的电脑服务器方法
- 看看node.js访问redis的库
- ajax访问Node服务器
- Neutron总结-security group
- 使用Java读取Mysql的DateTime类型数据
- 代码与编程题
- [cocos2d-x]TextureCache和spriteFramecache
- 如何用Eclipse在Resin中调试Web应用程序
- 关于别的电脑访问node.js建的服务器
- Dijkstra堆优化学习
- Session
- 剑指Offer算法题之已知两种遍历方式重建二叉树--面试题6:重建二叉树
- 使用单层感知器对坐标点进行二分类
- String的常用类
- JAVA关键字整理:static & final & native
- [Visual Studio]MFC游戏开发之回合制对战系统_by书訢
- Filter字符编码过滤器