Skip to content

Commit 979346c

Browse files
author
yunkui.zhou
committed
提取配置信息到config文件中
1 parent 93157cd commit 979346c

11 files changed

+76
-43
lines changed

bin/www

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
#!/usr/bin/env node
2-
if (process.env.NODE_ENV === 'local') {
3-
//由于运行时不会设置BABEL_ENV = node,单独为node运行时设置插件处理
4-
//这里如果统一设置BABEL_ENV = node,导致webpack内babel运行时也会去读取env: node的配置
5-
require('babel-register')({
6-
'plugins': [
7-
[
8-
'babel-plugin-transform-require-ignore',
9-
{
10-
extensions: ['.less']
11-
}
12-
],
13-
'dynamic-import-node'
14-
]
15-
})
16-
}
2+
const path = require('path')
3+
process.env['NODE_CONFIG_DIR'] = path.resolve(__dirname, '../server/config')
4+
const config = require('config')
5+
console.log(`读取配置文件${config.env}.js`)
176
const app = require('../server/index').default
187
const http = require('http')
198
const server = http.createServer(app)
20-
server.listen('8888', '127.0.0.1', () => {
9+
server.listen(config.port, config.ip)
10+
server.on('error', (error) => {
11+
throw error
12+
})
13+
server.on('listening', () => {
2114
console.log(`启动成功,监听端口:${server.address().port}`)
2215
})

clean.sh

-2
This file was deleted.

eslint.sh

-3
This file was deleted.

nodemon.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"execMap": {
1111
},
1212
"env": {
13-
"NODE_ENV": "local"
13+
"NODE_ENV": "development"
1414
},
1515
"watch": [
1616
"server/"

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
"name": "middleware",
33
"version": "0.0.1",
44
"scripts": {
5-
"fix": "sh ./eslint.sh",
65
"start": "nodemon ./bin/www",
7-
"clean": "sh ./clean.sh",
8-
"prebuild": "npm run clean && babel bin --out-dir dist/bin &&cross-env BABEL_ENV=node babel client --out-dir dist/client --copy-files && babel server --out-dir dist/server --copy-files",
6+
"prebuild": "rimraf ./dist && babel bin --out-dir dist/bin && cross-env BABEL_ENV=node babel client --out-dir dist/client --copy-files && babel server --out-dir dist/server --copy-files",
97
"build": "cross-env NODE_ENV=production webpack --config webpack.config.js",
108
"postbuild": "babel public --out-dir dist/public --copy-files && babel test --out-dir dist/test --copy-files"
119
},
@@ -19,6 +17,7 @@
1917
"babel-plugin-transform-require-ignore": "^0.1.1",
2018
"babel-preset-env": "^1.6.1",
2119
"babel-preset-react": "^6.24.1",
20+
"config": "^1.30.0",
2221
"cross-env": "^5.1.4",
2322
"css-loader": "^0.28.11",
2423
"eslint": "^4.19.1",
@@ -44,6 +43,7 @@
4443
"react-router-redux": "^5.0.0-alpha.9",
4544
"redux": "^3.7.2",
4645
"redux-thunk": "^2.2.0",
46+
"rimraf": "^2.6.2",
4747
"style-loader": "^0.20.3",
4848
"url-loader": "^1.0.1",
4949
"webpack": "^4.3.0",

server/config/beta.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
module.exports = {
3+
env: 'beta',
4+
port: '8888',
5+
ip: '0.0.0.0',
6+
}

server/config/dev.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
module.exports = {
3+
env: 'dev',
4+
port: '8888',
5+
ip: '0.0.0.0'
6+
}

server/config/development.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
require('babel-register')({
3+
'plugins': [
4+
[
5+
'babel-plugin-transform-require-ignore',
6+
{
7+
extensions: ['.less']
8+
}
9+
],
10+
'dynamic-import-node'
11+
]
12+
})
13+
const injectDevelopmentTools = (app) => {
14+
const config = require('../../webpack.config.local')
15+
const webpack = require('webpack')
16+
const compiler = webpack(config)
17+
const webpackDevMiddleware = require('webpack-dev-middleware')
18+
const webpackHotMiddleware = require('webpack-hot-middleware')
19+
app.use(webpackHotMiddleware(compiler, {
20+
log: false,
21+
path: '/__webpack_hmr',
22+
heartbeat: 2000
23+
}))
24+
app.use(webpackDevMiddleware(compiler, {
25+
publicPath: config.output.publicPath,
26+
serverSideRender: true
27+
}))
28+
return '本地开发链接webpack与node服务'
29+
}
30+
module.exports = {
31+
env: 'development',
32+
port: '8888',
33+
ip: '0.0.0.0',
34+
injectDevelopmentTools
35+
}

server/config/production.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
module.exports = {
3+
env: 'production',
4+
port: '8888',
5+
ip: '0.0.0.0'
6+
}

server/config/staging.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
module.exports = {
3+
env: 'staging',
4+
port: '8888',
5+
ip: '0.0.0.0'
6+
}

server/index.js

+4-18
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,13 @@
44
import express from 'express'
55
import path from 'path'
66
import pages from './routes/pages'
7+
const config = require('config')
78
const app = express()
9+
console.log(config.injectDevelopmentTools && config.injectDevelopmentTools(app))
810
app.set('views', path.join(__dirname, 'views'));
911
app.set('view engine', 'hbs')
10-
app.get(express.static(path.resolve(__dirname, '../public')))
12+
app.use(express.static(path.resolve(__dirname, '../public')))
1113

12-
if (process.env.NODE_ENV === 'local') {
13-
const config = require('../webpack.config.local')
14-
const webpack = require('webpack')
15-
const compiler = webpack(config)
16-
const webpackDevMiddleware = require('webpack-dev-middleware')
17-
const webpackHotMiddleware = require('webpack-hot-middleware')
18-
app.use(webpackHotMiddleware(compiler, {
19-
log: false,
20-
path: '/__webpack_hmr',
21-
heartbeat: 2000
22-
}))
23-
app.use(webpackDevMiddleware(compiler, {
24-
publicPath: config.output.publicPath,
25-
serverSideRender: true
26-
}))
27-
}
2814
//页面链式路由入口
2915
app.use('/p', pages)
3016
//首页
@@ -33,7 +19,7 @@ app.get('/', (req, res) => {
3319
title: 'react-redux-iapp同构项目'
3420
})
3521
})
36-
app.use((req, res, next, err) => {
22+
app.use((err, req, res, next) => {
3723
res.json({
3824
message: '404',
3925
err: err

0 commit comments

Comments
 (0)