mean/angular/express 打包

1. step 1: 修改 angular.json

修改 angular.json 文件中的 outputpath: dist/aaa-sys 改为 outputpath: pack/aaa (pack/aaa 是自定义的目录名)

2. step 2 : 修改environment.prod.ts文件

修改environment.prod.ts文件,增加apiUrl参数,如下所示:

export const environment = {
  production: true,
  apiUrl: 'http://localhost:3000/api'
};

注意:在部署到远程服务器的时候,localhost要改为主机对应的IP地址

3. step 3 : 运行 ng build - -prod 进行打包

执行命令的时候,可能会出现错误:appears “property 'controls' does not exist on type 'AbstractControl'
解决方法: 找到相应的.html文件,把.myForm.controls['address'].controls 格式改为.myForm.controls['address']['controls']

运行 ng build - -prod 重新进行打包

4. step 4 : 修改 app.js

修改 app.js

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/images', express.static(path.join('pack/images')));

在以上代码后面添加如下代码:

app.use('/', express.static(path.join('pack/aaa')));
app.use((req, res, next) => {
  res.sendFile(path.join(__dirname, "aaa","index.html"));
});

注意:aaa 是你打包的名称,部署的时候,只需要拷贝pack包即可

5. step 5:mean 启动

npm run start:server
start:server
package.json "start:server": "nodemon server.js"
server.js express 监听端口

const app = require("./pack/app");
const debug = require("debug")("node-angular");
const http = require("http");

const normalizePort = val => {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
};

const onError = error => {
  if (error.syscall !== "listen") {
    throw error;
  }
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  switch (error.code) {
    case "EACCES":
      console.error(bind + " requires elevated privileges");
      process.exit(1);
      break;
    case "EADDRINUSE":
      console.error(bind + " is already in use");
      process.exit(1);
      break;
    default:
      throw error;
  }
};

const onListening = () => {
  const addr = server.address();
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  debug("Listening on " + bind);
};
const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);

const server = http.createServer(app);
server.on("error", onError);
server.on("listening", onListening);
server.listen(port);

你可能感兴趣的:(mean/angular/express 打包)