服务器用的是阿里云最早期的服务器低配版本1Gcpu,512M内存,20G硬盘,1M带宽,平常只是用来做测试,目前只处理业务逻辑,网站的设计上尽量避免占用太多的带宽,
静态文件的储存用的是阿里云oss,100G空间,够放视频,图片什么的,
html/js/css文件用的是阿里云的云虚拟机,目前200M空间,月流量5个G,几块钱的东西反正放着也是浪费,就先利用起来过期了再换到oss上
数据库用的是家里内网穿透出来的服务器,各种安全防护做足,数据才是最重要的,嘿嘿
人就是这么折腾。前端页面只考虑最新谷歌浏览器,其他免谈
目前只是一个简单的基础框架,接下来慢慢的完善功能,目前需要做的是:
用到的文件:
//main.js文件
require('./socketIO')(require('http').createServer(createServer).listen(3000));
const GET = require('./get');
const POST = require('./post');
function createServer(req,res){
switch (req.method){
case "GET":GET(req,res); break;
case "POST":POST(req,res);break;
}
}
//get.js文件
var fs = require('fs');
module.exports= function (req,res) {
fs.readFile('./www/index.html',function(error,data){
res.writeHead(200,{'Content-Type':'text/html'});
res.end(data,'utf-8');
});
};
//post.js 文件
const dataAPI = require('./dataAPI');
const co=require('co');
const url = require('url');
const querystring=require('querystring');
var postAPI={
new: function (v,callback) {
const that=this;
return that[v]= function (req,res) {
var arr = [];
req.on("data",function(data){
arr.push(data);
});
req.on("end",function(){
var arg={
pathname:url.parse(req.url, true).pathname.slice(1),
query:JSON.parse(Buffer.concat(arr).toString()) ,
ret:function(ret) {
res.writeHead(200, { 'Content-Type': 'text/plain;charset=utf-8' });
res.write(ret);
res.end(null);
}
};
return co(callback(arg));
})
}
}
};
module.exports= function (req,res) {
var pathname=url.parse(req.url, true).pathname.slice(1);
if(pathname!='req'||'res'||'new'){
if(postAPI[pathname] instanceof Function){
postAPI[pathname](req,res);
}else{
console.log(pathname,'不是函数或接口错误');
}
}
};
postAPI.new("aaa", function *(arg) {
var redata= yield dataAPI.api({"textID" : "596da54ef708180c436d4128"});
arg.ret(JSON.stringify({query:arg.query,data:redata}));
});
//socketIO.js文件
const dataAPI = require('./dataAPI');
const co=require('co');
module.exports= function (server) {
var io = require('socket.io').listen(server);
io.sockets.on('connect',function(socket){
socket.on("aa", function (data,callback) {
console.log(data);
co(function *() {
var redata= yield dataAPI.api({"textID" : "596da54ef708180c436d4128"});
callback(redata);
});
});
});
};
dataAPI.js文件
var co=require('co');
const MongoClient = require('mongodb').MongoClient;
var urla = 'mongodb://@127.0.0.1:27017/abc';
var dataAPI={};
MongoClient.connect(urla,function(err, client) {//连接池,避免连接数上限限制
if(err){
console.log("MongoClient.connect错误:",err);
}
dataAPI.client=client;
});
dataAPI.new= function (v,callback) {
return dataAPI[v]= function () {
var arg=arguments;
return co( callback(dataAPI.client,arg));
}
};
dataAPI.new("api", function *(client,arg) {
var data0 = client.collection('copyText');
var redata=yield data0.find(arg[0]).toArray();
console.log('数据接口打印',redata);
return redata;
});
module.exports= dataAPI;
index.html文件
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript">
var ajax={
XMLHttp : new XMLHttpRequest(),
postJSON: function (json,callback) {
var that=this.XMLHttp;
that.open('POST',json.api,true);
that.setRequestHeader('Content-type','application/json; charset=utf-8');
that.onreadystatechange = function () {
if (that.readyState==4&&that.status==200) {
callback(that.responseText)
}
};
that.send(JSON.stringify(json.data));
}
};
script>
head>
<body>
<input type="button" onclick="aaa()" value="触发socket接口">
<input type="button" onclick="bbb()" value="触发ajaxPOST接口">
<div id="dd">div>
body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js">script>
<script>
var socket = io.connect('ws://127.0.0.1:3000/');
function aaa(){
socket.emit('aa',{a:1}, function (data) {
document.getElementById("dd").innerHTML+='' +JSON.stringify(data)+''
});
}
function bbb(){
ajax.postJSON({api:'aaa',data:{a:1,b:2,c:3}}, function (data) {
console.log(data);
});
}
script>
html>