Skip to content

Commit

Permalink
服务端
Browse files Browse the repository at this point in the history
  • Loading branch information
benzhe committed Dec 13, 2016
1 parent d0f59a5 commit bacd103
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
"extends": "ali"
};
69 changes: 25 additions & 44 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,30 @@
// Node APP
var PORT = 8124; //应用运行端口号
var http = require("http");
var url = require("url");
var fs = require("fs");
var path = require("path");
const fs = require('fs');
const koa = require('koa');
const https = require('https');
const serve = require('koa-static');
const ssl = require('koa-sslify');

var mime = require("./mime").types;
const PORT = 4443;
const app = koa();

var server = http.createServer(function (request, response) {
//解析请求路径
var pathname = decodeURIComponent(url.parse(request.url).pathname);
if (pathname.slice(-1) === "/") {
pathname = pathname + "index.html";
}
console.log(pathname)
var realPath = path.join("./", path.normalize(pathname.replace(//g, "")));
//判断路径是否存在
fs.exists(realPath, function (exists) {
if (!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("路径 "+ pathname + " 不存在!");
response.end();
} else {
//路径存在,读取文件并返回数据
fs.readFile(realPath, "binary", function(err, file) {
if (err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.end(err);
} else {
//读取文件后缀名
var ext = path.extname(realPath);
ext = ext ? ext.slice(1) : "unknown";
//根据文件后缀名查找对应的MIME类型值并返回
var contentType = mime[ext] || "text/plain";

response.writeHead(200, {"Content-Type": contentType});
response.write(file, "binary");
response.end();
}
});
}
});
app.use(ssl());
app.use(serve('.'));

// index page
app.use(function* (next) {
this.body = 'hello world from ' + this.request.url;
yield next;
});

server.listen(PORT);
// SSL options
const options = {
key: fs.readFileSync('cert/server.key'),
cert: fs.readFileSync('cert/server.crt')
};

// start the server
// http.createServer(app.callback()).listen(80);
https.createServer(options, app.callback()).listen(PORT);

//运行成功,输出提示
console.log("Server runing at port: " + PORT);
// 运行成功,输出提示
console.log(`Server runing at port: ${PORT}`);
19 changes: 19 additions & 0 deletions cert/server.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDBjCCAe4CCQCvMCgyWczZHjANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJB
VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0
cyBQdHkgTHRkMB4XDTE2MTIxMzEwMDU0NFoXDTE3MTIxMzEwMDU0NFowRTELMAkG
A1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0
IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
AMJdaYsqnwsL+JI2MIMWzA5E5SZTZNXaAoIIrkWhGeBldJLxcRnurK0hyQ64N7Dn
uOdI8uzPFCB3LZS7UPiHKvdN/f5aK06Xxubvh1trMT6mRr5I8ksaFRuH/fpjo4zD
4WjTRE/f6/SEuDvcgvBp+DIIzDVGH+wPhxRU8YiGFEoz/ES1WIT2IXMIhtTVfyTG
L/8A9vynIiTQGP/6KuhQt6cm2sD5/N7gUYFg/DFks5KntyFsHfRZGqYJ5gipD1p7
1PBZWi4KoJCYxZwhFCUcbbu75cJuKEwq7plWQx5XOYlFQIvgD8ky+/mz3S3SUhbP
UifElEpQyybvhQj3FCR8g78CAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAgScjqa2y
Pam2lQR0cW4iLCSX4tbtCtGhVPU45aS61dx+hM0RJYMPB50PtzISPF9qBqSYT2ul
0yBeEAkaNJs/M4vAb0aByqa6LZfii72wxDaadaSfZT+3DQzalLqvQ7bxnb8naSTe
WZHbkFU7R2pddctDZ5uMSMna1hRJhRnOp0St6Qx2+ZgfD771iULLGxeVtrijnbdp
HAKFuiTUHOpGtBKx7TQgdd+UrOTPrM4gRGdCuvU+vIBuA/OkS0UdvsmdsdBUgaW3
QMfcssF+hfthidwFILMx0TfExWJrlb7XBv5hy37iNIK+GFKIx/HNI8/kGGmjyd1f
1GsdVOR1dx4V5Q==
-----END CERTIFICATE-----
16 changes: 16 additions & 0 deletions cert/server.csr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-----BEGIN CERTIFICATE REQUEST-----
MIICijCCAXICAQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUx
ITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcN
AQEBBQADggEPADCCAQoCggEBAMJdaYsqnwsL+JI2MIMWzA5E5SZTZNXaAoIIrkWh
GeBldJLxcRnurK0hyQ64N7DnuOdI8uzPFCB3LZS7UPiHKvdN/f5aK06Xxubvh1tr
MT6mRr5I8ksaFRuH/fpjo4zD4WjTRE/f6/SEuDvcgvBp+DIIzDVGH+wPhxRU8YiG
FEoz/ES1WIT2IXMIhtTVfyTGL/8A9vynIiTQGP/6KuhQt6cm2sD5/N7gUYFg/DFk
s5KntyFsHfRZGqYJ5gipD1p71PBZWi4KoJCYxZwhFCUcbbu75cJuKEwq7plWQx5X
OYlFQIvgD8ky+/mz3S3SUhbPUifElEpQyybvhQj3FCR8g78CAwEAAaAAMA0GCSqG
SIb3DQEBBQUAA4IBAQCWWRm369C36MvrEfr4dHglCFNISSuqDeKgKuY4Cbc5WUQw
koTNAmjR1G+J/dr/hgDT/bxUrD6UBkUjIEnV+ewFcDWONFu9JhlywkljondrmjhW
l8M1KFsTtAxTD5M8cpOL2I0qi4gNC1rEBkln993bx9tXZf9jGojPDxta32vhYJ3p
WnGpFxIk6FHuAY3U3cMwA/inLTlfaYTtTBulwojabvG+3XBJpanGnodEeVyJJDqR
PMgKgnN1xeQxmQNykuQxwy50byhUC+ABDbzvO78vsc2mYT9pVk+WPQuTPXCj1eqq
VMSxiq7zbWx3YXdnDKLmcbWrNDMdqqQn+Qcpeu3n
-----END CERTIFICATE REQUEST-----
27 changes: 27 additions & 0 deletions cert/server.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAwl1piyqfCwv4kjYwgxbMDkTlJlNk1doCggiuRaEZ4GV0kvFx
Ge6srSHJDrg3sOe450jy7M8UIHctlLtQ+Icq9039/lorTpfG5u+HW2sxPqZGvkjy
SxoVG4f9+mOjjMPhaNNET9/r9IS4O9yC8Gn4MgjMNUYf7A+HFFTxiIYUSjP8RLVY
hPYhcwiG1NV/JMYv/wD2/KciJNAY//oq6FC3pybawPn83uBRgWD8MWSzkqe3IWwd
9FkapgnmCKkPWnvU8FlaLgqgkJjFnCEUJRxtu7vlwm4oTCrumVZDHlc5iUVAi+AP
yTL7+bPdLdJSFs9SJ8SUSlDLJu+FCPcUJHyDvwIDAQABAoIBAFuh0G1kxaBen2zH
EW3r5H3hqyq7bDGJ217UmtyGy/1Ht4LDQzAXnt7sq6492TuUs06OzMAqDHIV3a5d
9RmGGBKJeXJdrkcaNRKQ4H6P9aJ0n6t8FoajoEnffjR6nxNxEabq2yzRA5u+qmDG
YBDZ1MI59ZQ0ID6ZP3Sta0WS+KPWNLK6QXoMID3iEZESyDL7Q0+Z1ljfB3+mcXjM
fx5woq7y+MRCQyXToPYNFwB6T+gb45OcpBgaHrpZwZHBK40HoHJ2ExVeJi3HLjeY
2aVIg62yvLaaJZo/B1zHq4pWhUHauv/BQdrszpsth1oN23feT8J0Qe+6+HYL3mh0
iXQOyDkCgYEA+Z5kHE+tkpxoxm4mJXNg0k3NpBQWUY6JZ7MrA7uxejkpOaC93Dds
II9nVe6ffaiwTGRnw3TMJc54cFgZqlHuSb3nrSxn74srrv5+PCxmV5/rijmNHNBQ
8U1Ir5CrfKPx0iRHHddIPXpWPke4ZSVPnp34WCMPh1X0FLYbQKWV1w0CgYEAx1Vq
y2fSEDOHH4CCHfbM8RgSvK9NAr4MJ2JimInP6JSQIzuRnW0XjT24v3o7RNMTN7yC
gQ2hVQRwYEJH8gk22xxiK0AVBRZiPhcV9jAfvpxrYKs/tuBXnm25tPo4TEy9Dqb/
JrCHpN7MTbY1dEpyTn02OrY1AkkSIzT7NnZS0vsCgYEAgyJHm2vsVpIepOfm2FPV
sIW5G5KF6xCDdNv2I/7tjLUMyD5+n3rqLezIL6etV/lPrMIulMr+GHpwYSyXRxfF
pC3E0P/8Fia5qzP9IyV8GlpkBfiX/mQdsbaVEc5hmJgUCP6P8chQJiPnvM42u8Qv
ILgCaGL63HE2Sc8lX//dSN0Cf3vNkYbD9LQbTNFD/4ycvK0BRqHqPup1HxdXA+W5
smfB3cQEO2yLVRVkQFqskCMu9cCoVGXTEWC8YQ9oG0c4cdmkx1QVePiv7lSiAgHl
4dGBVS+1LN1oeQLtI2pD/rNk9gcjsVVWTukXd8zMXmbb1iLpocTVZhcFSz6SqXQq
CUcCgYEAh8HhUKanr9XavWxyJFqbjl3YcjfUxEe5Atu97lexka54xZpP5dj3eeyy
Kq7n5YnOToqgcr97FOcXRpZKOaBFd1NdhicwI26H2/7C8ap1e7DTUG5skZSLIXBp
nIzXLf7zDHl5wTExAsZ9HAMxCJwD2sTZKble/zKWDvdbB+OPWmw=
-----END RSA PRIVATE KEY-----
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"dependencies": {
"dat.gui": "^0.6.1",
"es6-promise": "^4.0.5",
"koa": "^1.2.4",
"koa-sslify": "^1.1.0",
"koa-static": "^2.0.0",
"stats.js": "^0.17.0",
"three": "^0.82.1",
"tween.js": "^16.3.5",
Expand All @@ -17,6 +20,12 @@
"devDependencies": {
"browserify": "latest",
"derequire": "latest",
"eslint": "^3.12.1",
"eslint-config-airbnb": "^13.0.0",
"eslint-config-ali": "^1.0.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^3.0.1",
"eslint-plugin-react": "^6.8.0",
"watchify": "latest"
},
"scripts": {
Expand Down

0 comments on commit bacd103

Please sign in to comment.