文件prod.server.js
// 依赖express ,config var express = require('express'); var config = require('./config/index'); // 当前环境变量下的port 如果没有就去config.build下找port var port = process.env.PORT || config.build.port; // 启动express var app = express(); // 定义路由 var router = express.Router(); // 当访问/时 路径指向index.html router.get('/', function (req, res, next) { req.url = '/index.html'; next(); }); app.use(router); // webpack.dev.conf.js中的配置 var appData = require('./data.json'); var seller = appData.seller; var goods = appData.goods; var ratings = appData.ratings; var apiRoutes = express.Router(); apiRoutes.get('/seller', function (req, res) { res.json({ errno: 0, data: seller }); }); apiRoutes.get('/goods', function (req, res) { res.json({ errno: 0, data: goods }); }); apiRoutes.get('/ratings', function (req, res) { res.json({ errno: 0, data: ratings }); }); app.use('/api', apiRoutes); // 定义static目录 app.use(express.static('./dist')); module.exports = app.listen(port, function (err) { if (err) { console.log(err); return } console.log('Listening at http://localhost:' + port + ' ') });
启动
node prod.server.js