Skip to content
This repository has been archived by the owner on Jul 31, 2018. It is now read-only.

Commit

Permalink
add api-server
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaijun committed Mar 15, 2016
1 parent 5e31a16 commit 010c952
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server -d --history-api-fallback --hot --inline --progress --colors --port 3000",
"build": "NODE_ENV=production webpack --progress --colors"
"start": "node server/index.js",
"build": "NODE_ENV=production webpack --progress --colors",
"serve": "npm run build & NODE_ENV=production npm run start"
},
"license": "MIT",
"devDependencies": {
Expand All @@ -17,23 +18,25 @@
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"babel-runtime": "^6.5.0",
"classnames": "^2.2.3",
"connect-history-api-fallback": "^1.1.0",
"css-loader": "^0.23.1",
"express": "^4.13.4",
"file-loader": "^0.8.5",
"postcss-loader": "^0.8.1",
"rucksack-css": "^0.8.5",
"style-loader": "^0.13.0",
"webpack": "^1.12.14",
"webpack-dev-server": "^1.14.1",
"webpack-hot-middleware": "^2.7.1",
"babel-runtime": "^6.5.0",
"classnames": "^2.2.3",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-hot-loader": "^1.3.0",
"react-redux": "^4.4.0",
"react-router": "^2.0.0",
"react-router-redux": "^4.0.0",
"redux": "^3.3.1",
"redux-actions": "^0.9.1"
"redux-actions": "^0.9.1",
"rucksack-css": "^0.8.5",
"style-loader": "^0.13.0",
"webpack": "^1.12.14",
"webpack-dev-server": "^1.14.1",
"webpack-hot-middleware": "^2.7.1"
}
}
47 changes: 47 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const express = require('express')
const webpack = require('webpack')
const WebpackDevServer = require('webpack-dev-server')
const webpackConfig = require('../webpack.config.js')
const history = require('connect-history-api-fallback');
const CLIENT_PORT = 3000
const SERVER_PORT = 3001

const app = express()

if(app.get('env') === 'development'){

//enable HotReplacementPlugin
webpackConfig.entry.jsx = [webpackConfig.entry.jsx]
webpackConfig.entry.jsx.unshift(`webpack-dev-server/client?http://localhost:${CLIENT_PORT}/`, "webpack/hot/dev-server")
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin())

const devServer= new WebpackDevServer(webpack(webpackConfig), {
contentBase: __dirname + '/../client', //seems doesn't effect
hot: true,
historyApiFallback: true,

//proxy the server port, so that easy to avoid cross-domain while requesting the APIs
proxy: {
"/api/*": `http://localhost:${SERVER_PORT}`
},

// webpack-dev-middleware options
stats: { colors: true },
}).listen(CLIENT_PORT)

}
else{
app.use(history())
app.use(express.static(__dirname + '/../static'))
}

app.get('/test', function (req, res) {
res.send('Hello World!')
});
app.get('/api/test', function (req, res) {
res.send('Hello World!')
});

app.listen(SERVER_PORT, function () {
console.log(`Example app listening on port ${SERVER_PORT}!`)
});

0 comments on commit 010c952

Please sign in to comment.