dev-server.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. require('./check-versions')()
  2. var config = require('../config')
  3. if (!process.env.NODE_ENV) {
  4. process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
  5. }
  6. var opn = require('opn')
  7. var path = require('path')
  8. var express = require('express')
  9. var webpack = require('webpack')
  10. var proxyMiddleware = require('http-proxy-middleware')
  11. var webpackConfig = require('./webpack.dev.conf')
  12. // default port where dev server listens for incoming traffic
  13. var port = process.env.PORT || config.dev.port
  14. // automatically open browser, if not set will be false
  15. var autoOpenBrowser = !!config.dev.autoOpenBrowser
  16. // Define HTTP proxies to your custom API backend
  17. // https://github.com/chimurai/http-proxy-middleware
  18. var proxyTable = config.dev.proxyTable
  19. var app = express()
  20. // 添加网上配置JSON
  21. // 本地json-server服务器搭建代码
  22. // 引入数据库文件
  23. var appData = require('../dist/static/api/viewAPI.json')
  24. // 引入数据库
  25. var getBoardList = appData.getBoardList
  26. var apiRoutes = express.Router()
  27. // 使用api的方法来创建连接时候的请求
  28. apiRoutes.post('/getBoardList', function (req, res) {
  29. res.json({
  30. errno: 0 ,
  31. data: getBoardList
  32. })
  33. })
  34. // 调用api
  35. app.use('/api', apiRoutes)
  36. var compiler = webpack(webpackConfig)
  37. var devMiddleware = require('webpack-dev-middleware')(compiler, {
  38. publicPath: webpackConfig.output.publicPath,
  39. quiet: true
  40. })
  41. var hotMiddleware = require('webpack-hot-middleware')(compiler, {
  42. log: () => {}
  43. })
  44. // force page reload when html-webpack-plugin template changes
  45. compiler.plugin('compilation', function (compilation) {
  46. compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
  47. hotMiddleware.publish({ action: 'reload' })
  48. cb()
  49. })
  50. })
  51. // proxy api requests
  52. Object.keys(proxyTable).forEach(function (context) {
  53. var options = proxyTable[context]
  54. if (typeof options === 'string') {
  55. options = { target: options }
  56. }
  57. app.use(proxyMiddleware(options.filter || context, options))
  58. })
  59. // handle fallback for HTML5 history API
  60. app.use(require('connect-history-api-fallback')())
  61. // serve webpack bundle output
  62. app.use(devMiddleware)
  63. // enable hot-reload and state-preserving
  64. // compilation error display
  65. app.use(hotMiddleware)
  66. // serve pure static assets
  67. var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
  68. app.use(staticPath, express.static('./static'))
  69. var uri = 'http://localhost:' + port
  70. devMiddleware.waitUntilValid(function () {
  71. console.log('> Listening at ' + uri + '\n')
  72. })
  73. module.exports = app.listen(port, function (err) {
  74. if (err) {
  75. console.log(err)
  76. return
  77. }
  78. // when env is testing, don't need open it
  79. if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
  80. opn(uri)
  81. }
  82. })