Browse Source

node 服务不稳定-1

master
sunshine 11 months ago
parent
commit
e5e5cbcaca
  1. 12
      README.md
  2. 8
      app.js
  3. 9
      modules/auto/MaterialAutoUpdate.js
  4. 7
      package.json
  5. 20
      pm2.json

12
README.md

@ -0,0 +1,12 @@
1: 安装pm2进程管理工具
npm install pm2@latest -g
pm2 install pm2-logrotate
2: 安装项目使用依赖
npm install
3: 启动方式
开发环境: npm run start:dev
测试环境: npm run start:test
预发环境: npm run start:stg
生产环境: npm run start:prod

8
app.js

@ -2,10 +2,7 @@ const Koa = require('koa');
const app = new Koa();
require('console-color-mr'); //颜色模块
const koaBody = require('koa-body'); //处理post请求参数
const fs = require('fs');
//环境配置
const config = require('./config/EnvApplication/'+ process.env.NODE_ENV);
app.use(koaBody({
multipart: true, // 支持文件上传
@ -19,9 +16,11 @@ app.use(autoUpdateRouter.allowedMethods());
// 字符串转换
const json = require('koa-json');
app.use(json());
// 捕获错误logger 记录
const logsUtil = require('./utils/LogUtil');
//环境配置
const config = require('./config/EnvApplication/'+ process.env.NODE_ENV);
// 错误处理中间件(注意:这不会捕获由 Koa 抛出的 'error' 事件)
app.use(async (ctx, next) => {
try {
@ -46,7 +45,6 @@ app.use(async (ctx, next) => {
};
}
});
// 捕获未被处理的错误(如异步错误或中间件外的错误)
app.on('error', (err, ctx) => {
logsUtil.logError(ctx, err);

9
modules/auto/MaterialAutoUpdate.js

@ -1,14 +1,11 @@
const autoUpdate = require ("./AutoUpdateSdkMain");
const path = require("path");
const fs = require("fs");
const axios = require('axios');
const downloadFile = require("../../utils/DownloadFile");
const logsUtil = require('../../utils/LogUtil');
//推送素材更新方法
const pushMaterial = async (ctx, next) => {
const pushMaterial = (ctx, next) => {
const req = ctx.query.req;
console.log('req', req);
logsUtil.logError(ctx, req)
autoUpdate.readArgvs(req);
};

7
package.json

@ -4,11 +4,10 @@
"description": "node server",
"main": "app.js",
"scripts": {
"start": "npm run start:dev",
"start:dev": "cross-env NODE_ENV=dev nodemon app.js",
"start:test": "cross-env NODE_ENV=test node app.js",
"start:stg": "cross-env NODE_ENV=stg node app.js",
"start:prod": "cross-env NODE_ENV=prod node app.js"
"start:test": "cross-env NODE_ENV=test pm2 start pm2.json",
"start:stg": "cross-env NODE_ENV=stg pm2 start pm2.json",
"start:prod": "cross-env NODE_ENV=prod pm2 start pm2.json"
},
"author": "",
"license": "ISC",

20
pm2.json

@ -0,0 +1,20 @@
{
"apps": [
{
"name": "design_node_koa",
"script": "app.js",
"log_date_format": "YYYY-MM-DD HH:mm Z",
"error_file": "logs/error.log",
"out_file": "logs/out.log",
"merge_logs": true,
"logrotate": {
"max_size": "10M",
"retain": 10,
"rotateInterval": "0 0 * * *",
"compress": true
},
"watch": true,
"ignore_watch": ["node_modules", "logs"]
}
]
}
Loading…
Cancel
Save