Skip to content
This repository was archived by the owner on May 21, 2020. It is now read-only.

Commit 11082ba

Browse files
committed
feat: real express middleware-like api
1 parent bb455bc commit 11082ba

File tree

4 files changed

+48
-51
lines changed

4 files changed

+48
-51
lines changed

src/server/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ if (isDev) {
2222
}
2323

2424
// I. Static Assets
25-
m.staticMiddleware(app)
25+
app.use(m.staticMiddleware)
2626
// II. API
27-
m.apiMiddleware(app)
27+
app.use(m.apiMiddleware)
2828
// III. Views
29-
m.viewMiddleware(app)
29+
app.use(m.viewMiddleware)
3030

3131
//
3232
// Initialise Express

src/server/middlewares/apiMiddleware.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@ import express from 'express'
99

1010
const API_URL = '/api/v1'
1111

12-
export default (app) => {
13-
// Parse application/json bodys
14-
app.use(bodyParser.json())
15-
// Parse application/x-www-form-urlencoded bodys
16-
app.use(bodyParser.urlencoded({ extended: true }))
17-
// create restful routes
18-
Object.keys(resources).forEach(makeResource)
12+
const app = express.Router()
1913

20-
function makeResource(routeName) {
21-
const router = express.Router()
14+
// Parse application/json bodys
15+
app.use(bodyParser.json())
16+
// Parse application/x-www-form-urlencoded bodys
17+
app.use(bodyParser.urlencoded({ extended: true }))
18+
// create restful routes
19+
Object.keys(resources).forEach(makeResource)
2220

23-
resources[routeName](router)
21+
function makeResource(routeName) {
22+
const router = express.Router()
2423

25-
app.use(`${API_URL}/${routeName}`, router)
26-
}
24+
resources[routeName](router)
25+
26+
app.use(`${API_URL}/${routeName}`, router)
2727
}
28+
29+
export default app

src/server/middlewares/staticMiddleware.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,4 @@ import express from 'express'
99
const MAX_AGE = isDev ? 0 : '1 year'
1010
const staticCache = { maxAge: MAX_AGE, etag: true, lastModified: false }
1111

12-
export default (app) => {
13-
// root dir assets
14-
app.use(express.static(outputPath, staticCache))
15-
}
12+
export default express.static(outputPath, staticCache)

src/server/middlewares/viewMiddleware.js

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,36 @@ import configureStore from 'configureStore'
1313
import Html from '../components/Html.jsx'
1414
import reducers from '../../shared/modules'
1515

16-
export default (app) => {
17-
// match everything else
18-
app.get('*', handleRequests)
19-
20-
function handleRequests({ url: location }, res) {
21-
match({ routes, location }, (err, redirect, renderProps) => {
22-
if (err)
23-
res.status(500).send(err.message)
24-
else if (redirect)
25-
res.status(302).redirect(redirect.pathname + redirect.search)
26-
else if (renderProps) {
27-
const store = configureStore(reducers)()
28-
const html = renderHtml(renderProps, store)
29-
30-
res.status(200).send(html)
31-
} else
32-
res.status(404).send('404')
33-
})
34-
}
35-
36-
function renderHtml(nextProps, store) {
37-
const initalState = store.getState()
38-
const provider = (
39-
createElement(Provider, { store },
40-
createElement(RouterContext, nextProps)
41-
)
42-
)
43-
const content = renderToString(provider)
44-
const markup = renderToStaticMarkup(
45-
createElement(Html, { content, initalState })
16+
function handleRequests({ url: location }, res) {
17+
match({ routes, location }, (err, redirect, renderProps) => {
18+
if (err) {
19+
res.status(500).send(err.message)
20+
} else if (redirect) {
21+
res.status(302).redirect(redirect.pathname + redirect.search)
22+
} else if (renderProps) {
23+
const store = configureStore(reducers)()
24+
const html = renderHtml(renderProps, store)
25+
26+
res.status(200).send(html)
27+
} else {
28+
res.status(404).send('404')
29+
}
30+
})
31+
}
32+
33+
function renderHtml(nextProps, store) {
34+
const initalState = store.getState()
35+
const provider = (
36+
createElement(Provider, { store },
37+
createElement(RouterContext, nextProps)
4638
)
39+
)
40+
const content = renderToString(provider)
41+
const markup = renderToStaticMarkup(
42+
createElement(Html, { content, initalState })
43+
)
4744

48-
return `<!doctype html>${markup}`
49-
}
45+
return `<!doctype html>${markup}`
5046
}
47+
48+
export default handleRequests

0 commit comments

Comments
 (0)