Skip to content

Commit 5263765

Browse files
author
yunkui.zhou
committed
添加readme;调整路由结构
1 parent 6c0d8c4 commit 5263765

File tree

20 files changed

+130
-62
lines changed

20 files changed

+130
-62
lines changed

.babelrc

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"syntax-dynamic-import"//不解析动态加载标识符 import() node环境无作用,得另想办法
1212
],
1313
"env": {
14+
//这个配置只针对服务端的客户端代码
1415
"node": {
1516
"plugins": [[
1617
"babel-plugin-transform-require-ignore",

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# server-side-render
2-
## for development
1+
# 服务端渲染同构框架
2+
3+
## 本地开发
34
> npm run start
4-
## for production
5+
6+
## 生产打包
57
> npm run build
6-
### start production server
78
8-
> node bin/www.js

bin/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## 启动文件
2+
3+
* www
4+
5+
> 服务器入口文件,里面针对开发环境node端的babel编译做了配置

bin/www

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
if (process.env.NODE_ENV === 'local') {
3-
//node运行时生效,同样也会去读.babelrc,plugins不会覆盖,会concat生效
4-
//webpack生效的配置是.babelrc
3+
//由于运行时不会设置BABEL_ENV = node,单独为node运行时设置插件处理
4+
//这里如果统一设置BABEL_ENV = node,导致webpack内babel运行时也会去读取env: node的配置
55
require('babel-register')({
66
'plugins': [
77
[

client/pages/example/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ReactDom from 'react-dom'
77
import {BrowserRouter} from 'react-router-dom'
88
import App from './App'
99
ReactDom.hydrate(
10-
<BrowserRouter basename='/components'>
10+
<BrowserRouter basename='/p/components'>
1111
<App/>
1212
</BrowserRouter>
1313
, document.getElementById('bd'))

client/pages/integration/App.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const App = (props) => {
1212
return (
1313
<Fragment>
1414
<div>
15-
<Link to='/home'>首页</Link>
15+
<Link to='/home'>个人主页</Link>
1616
</div>
1717
<div>
1818
<Link to='/center'>用户中心</Link>

client/pages/integration/Center/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import connect from '../../tools/connect'
77

88
class Center extends Component {
99
render () {
10-
return (<div>欢迎来到个人中心</div>)
10+
return (<div>{this.props.title}</div>)
1111
}
1212
}
1313
export default connect({

client/pages/integration/Home/index.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ import React, {Component} from 'react'
55
import connect from '../../tools/connect'
66
import * as actions from '../redux/action'
77
class Home extends Component {
8-
constructor(options) {
9-
super(options)
10-
}
118
componentDidMount() {
129
setTimeout(() => {
13-
this.props.actions.home('我是首页')
10+
this.props.actions.home('个人主页内容展示')
1411
//通过action出发路由跳转
1512
//this.props.actions.jump('center')
1613
}, 2000)

client/pages/integration/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import App from './App'
88
import storeFactory, {historyConf} from '../tools/store/forBrowser'
99
import reducers from './store/'
1010
const history = historyConf({
11-
basename: '/integration'
11+
basename: '/p/integration'
1212
})
1313
const store = storeFactory(reducers, history)
1414

client/pages/integration/redux/reducer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function center (state = {title: '欢迎来到个人中心'}, action) {
1010
}
1111
}
1212

13-
export function home (state = {title: '默认首页'}, action) {
13+
export function home (state = {title: '个人首页内容'}, action) {
1414
switch (action.type) {
1515
case 'home_title':
1616
return Object.assign({}, state, {title: action.data})

server/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 服务端文件说明
2+
3+
## index.js
4+
5+
> 服务端主文件,基础配置都在这里

server/index.js

+12-44
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,11 @@
33
*/
44
import express from 'express'
55
import path from 'path'
6-
7-
import React from 'react'
8-
import {renderToString} from 'react-dom/server'
9-
10-
import Component from '../client/pages/example/App'
11-
import extractMapping from './middleware/extractMapping'
12-
import mapAssets from './utils/mapAssets'
13-
import {StaticRouter} from 'react-router'
14-
15-
import {Provider} from 'react-redux'
16-
import store from '../client/pages/tools/store/forServer'
17-
import {Content} from '../client/pages/integration/Root/index'
18-
import Integration from '../client/pages/integration/App'
19-
import reducers from '../client/pages/integration/store/'
6+
import pages from './routes/pages'
207
const app = express()
218
app.set('views', path.join(__dirname, 'views'));
229
app.set('view engine', 'hbs')
23-
app.use(express.static(path.resolve(__dirname, '../public')))
10+
app.get(express.static(path.resolve(__dirname, '../public')))
2411

2512
if (process.env.NODE_ENV === 'local') {
2613
const config = require('../webpack.config.local')
@@ -38,37 +25,18 @@ if (process.env.NODE_ENV === 'local') {
3825
serverSideRender: true
3926
}))
4027
}
41-
42-
//以路径名称作为页面区分
43-
app.use('/Components', extractMapping, (req, res, next) => {
44-
//可以根据路径,针对某一个页面进行服务端渲染
45-
const content = renderToString(<StaticRouter
46-
location={req.url}
47-
context={{}}
48-
basename="/components">
49-
<Component/>
50-
</StaticRouter>)
51-
res.render('components', {
52-
app: content,
53-
links: mapAssets('components/index.css'),
54-
scripts: mapAssets('components/index.js')
28+
//页面链式路由入口
29+
app.use('/p', pages)
30+
//首页
31+
app.get('/', (req, res) => {
32+
res.render('homepage', {
33+
title: 'react-redux-iapp同构项目'
5534
})
5635
})
57-
58-
app.use('/integration', extractMapping, (req, res, next) => {
59-
const content = renderToString(<Provider store={store(reducers)}>
60-
<StaticRouter
61-
location={req.url}
62-
context={{}}
63-
basename="/integration"
64-
>
65-
<Content component={Integration}/>
66-
</StaticRouter>
67-
</Provider>)
68-
res.render('integration', {
69-
app: content,
70-
links: mapAssets('integration/index.css'),
71-
scripts: mapAssets('integration/index.js')
36+
app.use((req, res, next, err) => {
37+
res.json({
38+
message: '404',
39+
err: err
7240
})
7341
})
7442
export default app

server/middleware/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## 中间件
2+
3+
* extractMapping.js
4+
5+
> 用于读取webpack生成的资源映射文件

server/routes/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## 路由
2+
3+
* pages.js
4+
> 处理页面路由,服务端渲染之所在

server/routes/pages.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Created by ink on 2018/5/3.
3+
*/
4+
5+
import React from 'react'
6+
import {renderToString} from 'react-dom/server'
7+
import Component from '../../client/pages/example/App'
8+
import extractMapping from '../middleware/extractMapping'
9+
import mapAssets from '../utils/mapAssets'
10+
import {StaticRouter} from 'react-router'
11+
12+
import {Provider} from 'react-redux'
13+
import store from '../../client/pages/tools/store/forServer'
14+
import {Content} from '../../client/pages/integration/Root/index'
15+
import Integration from '../../client/pages/integration/App'
16+
import reducers from '../../client/pages/integration/store/'
17+
import express from 'express'
18+
const router = express.Router()
19+
20+
router.get('/', (req, res) => {
21+
res.status(301).set('Location', '/').end()
22+
})
23+
24+
router.get('/integration(/:page)?', extractMapping, (req, res, next) => {
25+
const content = renderToString(<Provider store={store(reducers)}>
26+
<StaticRouter
27+
location={req.originalUrl}
28+
context={{}}
29+
basename="/p/integration"
30+
>
31+
<Content component={Integration}/>
32+
</StaticRouter>
33+
</Provider>)
34+
res.render('integration', {
35+
app: content,
36+
links: mapAssets('integration/index.css'),
37+
scripts: mapAssets('integration/index.js')
38+
})
39+
})
40+
41+
router.get('/components(/:page)?', extractMapping, (req, res, next) => {
42+
//可以根据路径,针对某一个页面进行服务端渲染
43+
const content = renderToString(<StaticRouter
44+
location={req.originalUrl}
45+
context={{}}
46+
basename="/p/components">
47+
<Component/>
48+
</StaticRouter>)
49+
res.render('components', {
50+
app: content,
51+
links: mapAssets('components/index.css'),
52+
scripts: mapAssets('components/index.js')
53+
})
54+
})
55+
export default router

server/utils/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## 工具
2+
3+
* mapAssets.js
4+
> 用于生成script和link标签字符串

server/views/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## views
2+
3+
> 模板文件夹,使用的[hbs](https://github.com/pillarjs/hbs)

server/views/components.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<html>
22
<head>
33
<meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,initial-scale=1" />
4-
<title></title>
4+
<title>{{title}}</title>
55
{{{links}}}
66
</head>
77
<body>

server/views/homepage.hbs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<html>
2+
<head>
3+
<meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,initial-scale=1" />
4+
<title>{{title}}</title>
5+
<style>
6+
a{
7+
text-decoration: none;
8+
}
9+
</style>
10+
</head>
11+
<body>
12+
<nav style="text-align: center;padding: 20px 10px">
13+
<a href="/p/components">
14+
组件示例(不包含redux)
15+
</a>
16+
<a href="/p/integration">
17+
集成redux示例
18+
</a>
19+
</nav>
20+
</body>
21+
</html>

server/views/integration.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<html>
22
<head>
33
<meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,initial-scale=1" />
4-
<title></title>
4+
<title>{{title}}</title>
55
{{{links}}}
66
</head>
77
<body>

0 commit comments

Comments
 (0)