Skip to content

Commit

Permalink
添加实现结构和基本的服务器代码
Browse files Browse the repository at this point in the history
  • Loading branch information
sofish committed May 8, 2014
1 parent ff36e01 commit c28eb37
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
node_modules
build
.DS_Store
upload/*
27 changes: 27 additions & 0 deletions codes/post/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "Post",
"version": "0.0.1",
"main": "server/app.js",
"author": "sofish <[email protected]>",
"description": "your private cloud",
"keywords": [
"cloud",
"share",
"qiniu"
],

"license": {
"type": "MIT"
},

"scripts": {
"start": "node server/app.js"
},

"dependencies": {
"formidable": "1.2.11",
"mime": "1.0.14"
},

"readmeFilename": "readme.md"
}
Empty file added codes/post/src/public/app.css
Empty file.
Empty file added codes/post/src/public/app.js
Empty file.
Empty file.
70 changes: 70 additions & 0 deletions codes/post/src/server/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
var http = require('http')
, url = require('url')
, fs = require('fs')
, formidable = require('formidable')
, mime = require('mime')
, layout = fs.readFileSync(__dirname + '/view/layout')

// 发送数据
function send(res, options) {
res.writeHead(options.code, options.header);
res.write(options.data);
res.end();
}

// 生成页面
function render(name, message, code) {
var html, pathname = __dirname + '/view/' + name;

html = fs.existsSync(pathname) ? fs.readFileSync(pathname).toString() : '';
html = html.replace('{{message}}', message || '').replace();
html = layout.replace('{{body}}', html);

send(this, {
code: code || 200,
data: html,
header: {
'Content-Type': 'text/html',
'Content-Length': html.length
}
})
}

// 静态文件
function statics(pathname) {

// 没找到文件,或者他是一个文件夹
if(fs.existsSync(pathname) || !fs.statSync(pathname).isFile()) return send(this, { code: 404, data: ''});

var type = mime.lookup(pathname) || 'text/plain';

send(this, {
code: 200,
data: fs.readFileSync(pathname),
header: {
'Content-Type': type,
'Expires': 365 * 24 * 3600 + (new Date).getTime()
}
});
}

// 配置路由
http.createServer(function(req, res) {

var urlobj = url.parse(req.url)
, pathname = urlobj.pathname;

// 首页
if(pathname === '/') {
render.call(res, 'index');

// 静态文件处理
} else if (pathname.match(/^\/static\/.+/)) {
statics(pathname);

// 其他所有请求都直接 404
} else {
render.call(res, 'nofound', 'The document is not exist!', 404);
}

}).listen(7676);
Empty file added codes/post/src/view/index
Empty file.
Empty file added codes/post/src/view/layout
Empty file.
Empty file added codes/post/src/view/list
Empty file.
Empty file added codes/post/src/view/nofound
Empty file.
Empty file added codes/post/src/view/single
Empty file.
Empty file added codes/post/src/view/user
Empty file.

0 comments on commit c28eb37

Please sign in to comment.