Skip to content

Commit 84d0daf

Browse files
author
tanggc
committed
add(all): init
0 parents  commit 84d0daf

File tree

272 files changed

+7260
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

272 files changed

+7260
-0
lines changed

Diff for: .gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
**/*.log
2+
node_modules
3+
dist
4+
tmp
5+
.DS_Store
6+
.idea
7+
_*

Diff for: README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Angular 2 调查问卷示例项目
2+
3+
### 这是由广发证券互联网金融技术团队出品的原创书籍《揭秘 Angular 2》的第三部分项目实战的源代码。
4+
5+
![揭秘 Angular 2 封面](./angular_programming.jpg)
6+
7+
这个示例项目包含以下特点:
8+
9+
* 遵循官方最佳实践的目录布局
10+
* 代码难易程度适中,方便学习
11+
* 功能丰富的脚手架,易于扩展使用
12+
* 简洁化的后端服务,聚焦前端框架学习
13+
14+
15+
## 如何上手
16+
17+
调查问卷项目包括前端 frontend 目录以及后端 backend 目录。我们可以先运行后端服务,方便前端的注册与登录用户以及提供问卷相关的服务。安装过 Node.js 之后,在终端运行以下命令:
18+
19+
```bash
20+
cd backend
21+
npm install
22+
node app.js
23+
```
24+
25+
接下来,将终端目录定位 frontend 之中,再运行以下命令:
26+
27+
```bash
28+
npm install
29+
```
30+
31+
接着使用 `npm start` 即可运行整个项目代码。

Diff for: angular_programming.jpg

77.2 KB
Loading

Diff for: backend/app.js

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
const jsonServer = require('json-server')
2+
const uuid = require('node-uuid');
3+
const bodyParser = require('body-parser')
4+
const low = require('lowdb')
5+
const storage = require('lowdb/file-async')
6+
7+
// import jsonServer from 'json-server';
8+
// import uuid from 'node-uuid';
9+
// import bodyParser from 'body-parser';
10+
11+
// import low from 'lowdb';
12+
// import storage from 'lowdb/file-async';
13+
14+
var crypto = require('crypto');
15+
16+
//创建一个Express服务器
17+
const server = jsonServer.create();
18+
19+
//使用json-server默认选择的中间件(logger,static, cors和no-cache)
20+
server.use(jsonServer.defaults());
21+
22+
//使用body-parser中间件
23+
server.use(bodyParser.json());
24+
25+
26+
//数据文件
27+
const dbfile = process.env.prod === '1' ? 'db.json' : '_db.json';
28+
29+
//创建一个lowdb实例
30+
const db = low(dbfile, {storage});
31+
32+
33+
34+
var md5 = function(str) {
35+
return crypto
36+
.createHash('md5')
37+
.update(str.toString())
38+
.digest('hex');
39+
};
40+
41+
//添加新问卷
42+
server.post('/questionnaire/add', (req, res) => {
43+
const item = req.body;
44+
item.id = uuid.v1();
45+
item.createDate = new Date().toLocaleDateString();
46+
db('questionnaires').push(item).then(() => {
47+
res.json({'success':true, data:item});
48+
});
49+
});
50+
51+
//删除已有问卷
52+
server.get('/questionnaire/delete/:id', (req, res)=>{
53+
db('questionnaires').remove({id: req.params.id}).then(()=>{
54+
res.json({'success': true});
55+
});
56+
});
57+
58+
//获取所有问卷
59+
server.get('/questionnaires', (req, res) => {
60+
const questionnaires = db('questionnaires');
61+
res.json({'success':true, data:questionnaires});
62+
});
63+
64+
//根据id获取问卷数据
65+
server.get('/questionnaire/:id', (req, res) => {
66+
const questionnaire = db('questionnaires').find({id: req.params.id});
67+
res.json({'success':true, data:questionnaire});
68+
});
69+
70+
//更新已有问卷
71+
server.post('/questionnaire/update', (req, res) => {
72+
const item = req.body;
73+
db('questionnaires').chain().find({id:item.id}).assign(item).value();
74+
res.json({'success':true, data:item});
75+
});
76+
77+
//发布问卷
78+
server.get('/questionnaire/publish/:id', (req, res)=>{
79+
const item = db('questionnaires').chain().find({id:req.params.id});
80+
item.assign({state:1}).value();
81+
res.json({'success':true, data:item});
82+
});
83+
84+
// get userinfo
85+
server.get('/user/:username', function(req, res) {
86+
var user = db('user')
87+
.find({
88+
username: req.params.username
89+
});
90+
91+
res.json({
92+
success: true,
93+
data: {
94+
username: user.username,
95+
createDate: user.createDate
96+
}
97+
});
98+
});
99+
100+
// register
101+
server.post('/user/add', function(req, res) {
102+
var item = req.body;
103+
var user = db('user')
104+
.find({
105+
username: item.username
106+
});
107+
if (user) {
108+
res.json({
109+
success: false,
110+
message: '"' + item.username + '" is exists'
111+
})
112+
} else {
113+
item.password = md5(item.password);
114+
item.createDate = new Date().toLocaleDateString();
115+
db('user')
116+
.push(item)
117+
.then(function() {
118+
res.json({
119+
success: true
120+
});
121+
});
122+
}
123+
});
124+
125+
// login
126+
server.post('/login', function(req, res) {
127+
var data = req.body || {};
128+
var username = data.username;
129+
var user = db('user')
130+
.find({
131+
username: username
132+
});
133+
134+
if (user && user.password === md5(data.password)) {
135+
// todo reset session
136+
res.json({
137+
success: true
138+
});
139+
} else {
140+
res.json({
141+
success: false,
142+
message: 'username or password error'
143+
});
144+
}
145+
});
146+
147+
//路由配置
148+
const router = jsonServer.router(dbfile);
149+
server.use('/api', router);
150+
151+
//启动服务,并监听5000端口
152+
server.listen(5000, () => {
153+
console.log('server is running at ', 5000, dbfile);
154+
});

Diff for: backend/package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "server",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"body-parser": "^1.14.2",
13+
"json-server": "^0.8.7",
14+
"lowdb": "^0.12.2",
15+
"node-uuid": "^1.4.7"
16+
}
17+
}

Diff for: frontend/.docker/angular-seed.development.dockerfile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM node:6.6
2+
3+
# prepare a user which runs everything locally! - required in child images!
4+
RUN useradd --user-group --create-home --shell /bin/false app
5+
6+
ENV HOME=/home/app
7+
WORKDIR $HOME
8+
9+
ENV APP_NAME=angular-seed
10+
11+
# before switching to user we need to set permission properly
12+
# copy all files, except the ignored files from .dockerignore
13+
COPY . $HOME/$APP_NAME/
14+
RUN chown -R app:app $HOME/*
15+
16+
USER app
17+
WORKDIR $HOME/$APP_NAME
18+
19+
RUN npm install

Diff for: frontend/.docker/angular-seed.production.dockerfile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM node:6.6
2+
3+
# prepare a user which runs everything locally! - required in child images!
4+
RUN useradd --user-group --create-home --shell /bin/false app
5+
6+
ENV HOME=/home/app
7+
WORKDIR $HOME
8+
9+
ENV APP_NAME=angular-seed
10+
11+
# before switching to user we need to set permission properly
12+
# copy all files, except the ignored files from .dockerignore
13+
COPY . $HOME/$APP_NAME/
14+
RUN chown -R app:app $HOME/*
15+
16+
USER app
17+
WORKDIR $HOME/$APP_NAME
18+
19+
RUN npm install

Diff for: frontend/.docker/nginx.conf

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
map $http_upgrade $connection_upgrade {
2+
default upgrade;
3+
'' close;
4+
}
5+
6+
server {
7+
8+
listen ${NGINX_PORT};
9+
10+
server_name ${NGINX_HOST};
11+
# ssl on;
12+
# ssl_certificate angular-seed_server.crt;
13+
# ssl_certificate_key angular-seed_server.pem;
14+
#
15+
# ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
16+
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
17+
# ssl_prefer_server_ciphers on;
18+
# ssl_session_cache shared:SSL:10m;
19+
# ssl_session_tickets off; # Requires nginx >= 1.5.9
20+
# add_header Strict-Transport-Security "max-age=63072000; preload";
21+
# add_header X-Frame-Options DENY;
22+
#
23+
# location /api/microservice1 {
24+
# rewrite ^/api/microservice1/(.*)$ /$1 break;
25+
# proxy_pass https://microservice1/;
26+
# proxy_http_version 1.1;
27+
# proxy_set_header X-Forwarded-For $remote_addr;
28+
# }
29+
30+
location / {
31+
root /var/www/dist/prod;
32+
try_files $uri /index.html;
33+
index index.html;
34+
gzip on;
35+
gzip_types text/css text/javascript application/x-javascript application/json;
36+
}
37+
38+
}

Diff for: frontend/.dockerignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# compiled output
2+
dist
3+
tmp
4+
5+
# dependencies
6+
node_modules
7+
bower_components
8+
9+
# IDEs and editors
10+
.idea
11+
.vscode
12+
.project
13+
.classpath
14+
*.launch
15+
.settings/
16+
17+
# misc
18+
.sass-cache
19+
connect.lock
20+
coverage/*
21+
libpeerconnection.log
22+
npm-debug.log
23+
testem.log
24+
typings
25+
26+
# e2e
27+
e2e/*.js
28+
e2e/*.map
29+
30+
#System Files
31+
.DS_Store
32+
Thumbs.db

Diff for: frontend/.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
indent_style = space
8+
indent_size = 2
9+
end_of_line = lf
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true
12+
13+
[*.md]
14+
insert_final_newline = false
15+
trim_trailing_whitespace = false

Diff for: frontend/.github/CONTRIBUTING.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Submitting Pull Requests
2+
3+
**Please follow these basic steps to simplify pull request reviews - if you don't you'll probably just be asked to anyway.**
4+
5+
* Please rebase your branch against the current master
6+
* Run ```npm install``` to make sure your development dependencies are up-to-date
7+
* Please ensure that the test suite passes **and** that code is lint free before submitting a PR by running:
8+
* ```npm test```
9+
* If you've added new functionality, **please** include tests which validate its behaviour
10+
* Make reference to possible [issues](https://github.com/mgechev/angular2-seed/issues) on PR comment
11+
12+
## Submitting bug reports
13+
14+
* Please detail the affected browser(s) and operating system(s)
15+
* Please be sure to state which version of node **and** npm you're using

Diff for: frontend/.github/ISSUE_TEMPLATE.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!--
2+
IF YOU DON'T FILL OUT THE FOLLOWING INFORMATION WE MIGHT CLOSE YOUR ISSUE WITHOUT INVESTIGATING
3+
-->
4+
5+
**I'm submitting a ...** (check one with "x")
6+
```
7+
[ ] bug report => search github for a similar issue or PR before submitting
8+
[ ] feature request
9+
[ ] support request => Please do not submit support request here, instead see use [gitter](https://gitter.im/mgechev/angular2-seed) or [stackoverflow](https://stackoverflow.com/questions/tagged/angular2)
10+
```
11+
12+
**Current behavior**
13+
<!-- Describe how the bug manifests. -->
14+
15+
**Expected behavior**
16+
<!-- Describe what the behavior would be without the bug. -->
17+
18+
**Minimal reproduction of the problem with instructions**
19+
<!--
20+
If the current behavior is a bug or you can illustrate your feature request better with an example,
21+
please provide the *STEPS TO REPRODUCE* and if possible a *MINIMAL DEMO* of the problem.
22+
-->
23+
24+
**What is the motivation / use case for changing the behavior?**
25+
<!-- Describe the motivation or the concrete use case -->
26+
27+
**Please tell us about your environment:**
28+
<!-- Operating system, IDE, package manager, HTTP server, ... -->
29+
30+
* **Angular Seed Version:** `aaaaf75`
31+
<!-- Check which is the hash of the last commit from angular-seed that you have locally -->
32+
33+
* **Node:** `node --version` =

0 commit comments

Comments
 (0)