Skip to content

Commit fe8950b

Browse files
committed
node server 0.0.1
1 parent 8d39ecb commit fe8950b

18 files changed

Lines changed: 445 additions & 1 deletion

.gitignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Numerous always-ignore extensions
2+
*.bak
3+
*.patch
4+
*.diff
5+
*.err
6+
7+
# temp file for git conflict merging
8+
*.orig
9+
*.log
10+
*.rej
11+
*.swo
12+
*.swp
13+
*.zip
14+
*.vi
15+
*~
16+
*.sass-cache
17+
*.tmp.html
18+
*.dump
19+
20+
# OS or Editor folders
21+
.DS_Store
22+
._*
23+
.cache
24+
.project
25+
.settings
26+
.tmproj
27+
*.esproj
28+
*.sublime-project
29+
*.sublime-workspace
30+
nbproject
31+
thumbs.db
32+
*.iml
33+
34+
# Folders to ignore
35+
.happypack/
36+
dist/
37+
node_modules/
38+
jscoverage_lib/
39+
.imdone/
40+
41+
# Files to ignore
42+
.eslintrc.js
43+
.node-dev.json
44+
.hg
45+
.svn
46+
.CVS
47+
.idea

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# node-server-demo
2-
基于koa的前后端分离服务器脚本demo
2+
基于Koa的前后端分离服务端脚本demo

app.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
const Koa = require('koa');
3+
const staticServe = require('koa-static');
4+
const mount = require('koa-mount');
5+
const render = require('koa-ejs');
6+
const path = require('path');
7+
const logger = require('koa-logger');
8+
9+
const staticDirectory = staticServe(__dirname + '/src/public');
10+
const router = require('./src/routes');
11+
const config = require('./config');
12+
const app = new Koa();
13+
14+
// 设置模板引擎
15+
render(app, {
16+
root: path.join(__dirname, '/src/views'),
17+
viewExt: 'ejs',
18+
layout: false
19+
});
20+
21+
app .use(logger())
22+
.use(mount('/static', staticDirectory))
23+
.use(router.routes());
24+
25+
app.listen(config.listenPort, '0.0.0.0');

config/dev.env.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
// MySql配置
4+
const mysqlConfig = {
5+
host: '127.0.0.1',
6+
user: 'xxx',
7+
password: 'xxx',
8+
database: 'xxx',
9+
port: 3306
10+
};
11+
12+
// app运行端口号
13+
const listenPort = 8080;
14+
15+
module.exports = {
16+
mysqlConfig,
17+
listenPort
18+
}

config/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
const isProdEnv = process.env.NODE_ENV && 'production'=== process.env.NODE_ENV ;
4+
5+
let config = {};
6+
7+
if (isProdEnv) {
8+
config = require('./prod.env.config');
9+
} else {
10+
config =require('./dev.env.config');
11+
}
12+
13+
module.exports = config;

config/prod.env.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
// MySql配置
4+
const mysqlConfig = {
5+
host: '127.0.0.1',
6+
user: 'xxx',
7+
password: 'xxx',
8+
database: 'xxx',
9+
port: 3306
10+
};
11+
12+
// app运行端口号
13+
const listenPort = 80;
14+
15+
module.exports = {
16+
mysqlConfig,
17+
listenPort
18+
}

config/test.env.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
// MySql配置
4+
const mysqlConfig = {
5+
host: '127.0.0.1',
6+
user: 'xxx',
7+
password: 'xxx',
8+
database: 'xxx',
9+
port: 3306
10+
};
11+
12+
// app运行端口号
13+
const listenPort = 3000;
14+
15+
module.exports = {
16+
mysqlConfig,
17+
listenPort
18+
}

package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "node-server-demo",
3+
"author": "EragonJiang",
4+
"version": "1.0.0",
5+
"description": "Node server demo based on koa",
6+
"keywords": [
7+
"node",
8+
"koa",
9+
"demo"
10+
],
11+
"scripts": {
12+
"start": "NODE_ENV=production node app.js",
13+
"dev": "NODE_ENV=development node-dev --no-notify app.js",
14+
"lint": "eslint ./src --fix"
15+
},
16+
"dependencies": {
17+
"koa": "^2.3.0",
18+
"koa-csrf": "^2.3.0",
19+
"koa-ejs": "^4.1.0",
20+
"koa-logger": "^1.3.0",
21+
"koa-mount": "^3.0.0",
22+
"koa-onerror": "^3.1.0",
23+
"koa-router": "^5.1.2",
24+
"koa-session": "^3.3.1",
25+
"koa-static": "^4.0.1",
26+
"koa-views": "^6.0.2",
27+
"then-request": "^4.1.0"
28+
},
29+
"devDependencies": {
30+
"eslint": "^3.0.0",
31+
"eslint-config-airbnb": "^15.1.0",
32+
"eslint-plugin-import": "^2.7.0",
33+
"eslint-plugin-jsx-a11y": "^5.1.1",
34+
"eslint-plugin-react": "^7.1.0",
35+
"node-dev": "^3.1.3"
36+
},
37+
"license": "MITs",
38+
"engines": {
39+
"node": ">=6.0.0"
40+
},
41+
"repository": {
42+
"type": "git",
43+
"url": "https://github.com/Dragon-Rider/node-server-demo.git"
44+
}
45+
}

src/common/ENUM.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 常用数据定义
3+
*/
4+
5+
module.exports = {
6+
/* TABLE_PROJECT: config.mysql.tablePrefix + 'project', */
7+
8+
// 返回的业务码
9+
RET_SUCCESS: {
10+
code: 200,
11+
msg: '接口调用成功',
12+
},
13+
RET_Error: {
14+
code: 401,
15+
msg: '用户输入错误',
16+
},
17+
RET_FORBIDDEN: {
18+
code: 403,
19+
msg: '没有权限访问',
20+
},
21+
22+
// 获取ip信息的服务接口地址
23+
IP_INFO_SERVER: 'http://ip.taobao.com/service/getIpInfo.php?ip=',
24+
};

src/common/utils.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* 脚手架文件,各种公用方法集合
3+
* Created by Eragon on 2017-08-18
4+
*/
5+
const ENUM = require('./ENUM');
6+
7+
module.exports = {
8+
/**
9+
* getDomain 根据pageUrl获取domain
10+
* @param {string} url url
11+
* @return {string} domain
12+
*/
13+
getDomain(url) {
14+
const urlWithoutProtocol = url.split('//')[1];
15+
const domainEndPos = urlWithoutProtocol.indexOf('/');
16+
17+
if (domainEndPos === -1) {
18+
return urlWithoutProtocol;
19+
}
20+
21+
return urlWithoutProtocol.substring(0, domainEndPos);
22+
},
23+
24+
/**
25+
* isAjax 判定是否ajax请求
26+
* @param {object} ctx 对象
27+
* @return {Boolean} 是否是ajax请求
28+
*/
29+
isAjax(ctx) {
30+
return ('xmlhttprequest' === ((ctx.get('X-Requested-With') || '').toLowerCase())) || (ctx.get('accept').indexOf('json') > -1); // eslint-disable-line
31+
},
32+
33+
/**
34+
* getPageUrlWithoutParams 通过url获得不带参数的url
35+
* @param {string} url 原url
36+
* @return {string} 去除参数的url
37+
*/
38+
getPageUrlWithoutParams(url) {
39+
const queryPos = url.indexOf('?');
40+
const hashPos = url.indexOf('#');
41+
let pos = 0;
42+
43+
if (queryPos === -1 && hashPos === -1) {
44+
return url;
45+
} else if (queryPos !== -1 && hashPos === -1) {
46+
pos = queryPos;
47+
} else {
48+
pos = Math.min(queryPos, hashPos);
49+
}
50+
51+
return url.substring(0, pos);
52+
}
53+
};
54+

0 commit comments

Comments
 (0)