Skip to content

Commit

Permalink
静态服务器搭建好了
Browse files Browse the repository at this point in the history
  • Loading branch information
ksky521 committed May 5, 2013
1 parent 00eb047 commit 160df64
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bin/nodeppt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var action = argv.slice(2, 3).toString();

if (typeof nodePPT[action] === 'function') {
nodePPT.cwd = process.cwd();
nodePPT[action](argv.slice(2));
nodePPT[action](argv.slice(3));
} else {
nodePPT.help();
}
Expand Down
20 changes: 20 additions & 0 deletions lib/mime.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"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"
}
9 changes: 9 additions & 0 deletions lib/nodePPT.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ var ppt = module.exports = {
},
template: function(args) {
var opts = {};
var filename = args.slice(-1).toString();
if (filename !== '') {
if (gFile.exists(rootDir + '/ppts/' + filename + '.html')) {
console.log('ERROR: '.bold.red+'"'+filename+'" 已经存在!');
}
opts.filename = filename;
templateQ.splice(0, 1);
}

console.log('请回答以下问题:'.bold.green);
var v = templateQ.unshift();
(function next(prompt) {
Expand Down
63 changes: 60 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
var mimes = require(__dirname + '/mime.json');
var fs = require('fs');
var URL = require('url');
var path = require('path');
module.exports.start = function(port) {
port = parseInt(port, 10) || 8080;
var io = require('socket.io').listen(port, {
var app = startApp(port);
var io = require('socket.io').listen(app, {
// log: false,
origins: '*:*' //解决同源策略
}, function() {
console.log('start server @ localhost:' + port);
console.log('start socket server @ localhost:' + port);
});



//控制端
ctrlSocket = io.of('/control')
.on('connection', function(socket) {
Expand Down Expand Up @@ -65,4 +72,54 @@ module.exports.start = function(port) {
});
});
});
}
}

function startApp(port) {
var staticDir = path.join(__dirname, '../src') + '/';
var pptDir = path.join(__dirname, '../ppts') + '/';
var app = require('http').createServer(function(req, res) {
var url = URL.parse(req.url).path;
var dirname = path.dirname(url);
var realPath, ext;
if (dirname === '/ppt') {
realPath = pptDir + path.basename(url, '.html') + '.html';
console.log(realPath)
ext = 'html';
} else {
realPath = staticDir + url;
ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
}


//静态资源
if (fs.existsSync(realPath)) {
fs.readFile(realPath, 'binary', function(err, file) {
if (err) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end(err);
} else {
res.writeHead(200, {
'Content-Type': mimes[ext]
});
res.write(file, 'binary');
res.end();
}

});

} else {
res.writeHead(404, {
'Content-Type': mimes.txt
});

res.write('This request URL ' + url + ' was not found on this server.');
res.end();
}

}).listen(port);

return app;
}
4 changes: 2 additions & 2 deletions lib/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<meta charset="UTF-8">
<title><%= title %> - By <%= speaker %></title>
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="stylesheet" media="all" href="../src/css/nodeppt.css">
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="../src/css/phone.css">
<link rel="stylesheet" media="all" href="/css/nodeppt.css">
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="/css/phone.css">
</head>
<body>

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"url": "git://github.com/ksky521/nodePPT"
},
"dependencies": {
"express": "~3.2.2",
"read": "~1.0.4",
"colors": "~0.6.0-1",
"socket.io": "~0.9.13"
Expand Down
60 changes: 60 additions & 0 deletions ppts/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!--
Powered By nodePPT
version: 0.2.5
site: https://github.com/ksky521/nodePPT
date: 2013-05-05
-->
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>nodePPT Demo - By 三水清</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="stylesheet" media="all" href="/css/nodeppt.css">
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="/css/phone.css">
</head>
<body>

<slides id="container" class="layout-widescreen">
<slide class="slide ">
<article class="flexbox vcenter">
<h1>nodePPT Demo</h1>
<h3><a href="#">三水清</a></h3>
</article>
</slide>
<slide class="slide">
<hgroup>
<h2>目录</h2>
</hgroup>
<article>
<ul>
<li>这里是目录。。</li>
</ul>
</article>
</slide>

<slide class="slide thank-you-slide segue nobackground">
<article class="flexbox vleft auto-fadein">
<h2>&lt;Thank You!&gt;</h2>
</article>
</slide>
<slide class="slide">
<hgroup>
<h2>参考资料</h2>
</hgroup>
<article>
<ul>
<li><a href="https://github.com/ksky521/nodePPT" target="_blank">Powered By nodePPT</a></li>
</ul>
</article>
</slide>
<div class="slideTip" id="tip"></div>
<canvas id="drawBoard" class="draw-board" width="900" height="700"></canvas>
</slides>
<div class="progress"><span id="progress"></span></div>

<script src="https://gist.github.com/ksky521/5519468/raw/17ebd7450d7605c6e320206aaf5790a6cdd76d8e/gistfile1.js"></script>
<script>
</script>
</body>
</html>

0 comments on commit 160df64

Please sign in to comment.