dev-server.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. require('./check-versions')()
  2. var config = require('../config')
  3. if (!process.env.NODE_ENV) process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
  4. var path = require('path')
  5. var express = require('express')
  6. var webpack = require('webpack')
  7. var opn = require('opn')
  8. var proxyMiddleware = require('http-proxy-middleware')
  9. var webpackConfig = require('./webpack.dev.conf')
  10. // default port where dev server listens for incoming traffic
  11. var port = process.env.PORT || config.dev.port
  12. // Define HTTP proxies to your custom API backend
  13. // https://github.com/chimurai/http-proxy-middleware
  14. var proxyTable = config.dev.proxyTable
  15. var app = express()
  16. var appData = require('../data.json')
  17. var seller = appData.seller
  18. var goods = appData.goods
  19. var ratings = appData.ratings
  20. var apiRoutes = express.Router()
  21. apiRoutes.get('/seller', function (req, res) {
  22. res.json({
  23. errno: 0,
  24. data: seller
  25. });
  26. })
  27. apiRoutes.get('/goods', function (req, res) {
  28. res.json({
  29. errno: 0,
  30. data: goods
  31. })
  32. })
  33. apiRoutes.get('/ratings', function (req, res) {
  34. res.json({
  35. errno: 0,
  36. data: ratings
  37. });
  38. })
  39. app.use('/api', apiRoutes)
  40. var compiler = webpack(webpackConfig)
  41. var devMiddleware = require('webpack-dev-middleware')(compiler, {
  42. publicPath: webpackConfig.output.publicPath,
  43. quiet: true
  44. })
  45. var hotMiddleware = require('webpack-hot-middleware')(compiler, {
  46. log: () => {
  47. }
  48. })
  49. // force page reload when html-webpack-plugin template changes
  50. compiler.plugin('compilation', function (compilation) {
  51. compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
  52. hotMiddleware.publish({action: 'reload'})
  53. cb()
  54. })
  55. })
  56. // proxy api requests
  57. Object.keys(proxyTable).forEach(function (context) {
  58. var options = proxyTable[context]
  59. if (typeof options === 'string') {
  60. options = {target: options}
  61. }
  62. app.use(proxyMiddleware(context, options))
  63. })
  64. // handle fallback for HTML5 history API
  65. app.use(require('connect-history-api-fallback')())
  66. // serve webpack bundle output
  67. app.use(devMiddleware)
  68. // enable hot-reload and state-preserving
  69. // compilation error display
  70. app.use(hotMiddleware)
  71. // serve pure static assets
  72. var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
  73. app.use(staticPath, express.static('./static'))
  74. var uri = 'http://localhost:' + port
  75. devMiddleware.waitUntilValid(function () {
  76. console.log('> Listening at ' + uri + '\n')
  77. })
  78. module.exports = app.listen(port, function (err) {
  79. if (err) {
  80. console.log(err)
  81. return
  82. }
  83. // when env is testing, don't need open it
  84. if (process.env.NODE_ENV !== 'testing') {
  85. opn(uri)
  86. }
  87. })