<commit_before>const log = require('t-log');
const express = require('express');
const coc = require('../index');

const app = express();

const cocer = coc(app, {
  interceptorDir: `${__dirname}/interceptors`,
  routerDir: `${__dirname}/routers`,
  viewDir: `${__dirname}/views`
});


// 查看阶段列表，每个阶段都可以用before,after处理
log.info(cocer.stageNames);

cocer.before('pageInfo', (req, res, next) => {
  req.reqCircle = log.time();
  next();
});
cocer.before('initHttpRequest', (req, res, next) => {
  let timeout = 5 * 1000;

  if (req.router && req.router.timeout) {
    timeout = req.router.timeout;
  }
  req.httpRequestConfig = { timeout };

  next();
});

// forward has been test
cocer.after('requestProxy', (req, res, next) => {
  const pathname = req.pathname;
  log.debug('param:', req.param);
  log.debug('path:', pathname);

  if (pathname === '/forward') {
    log.info(req.router, '********');
    res.forward('/proxy');
    // res.redirect('http://www.163.com');
    // res.status(200).send('hello 200 ok!');
    // next已经可以正确处理forward情况
    // 类似redirect，res.send等已经响应了服务器的情况
    next();
    return;
  }

  next();
});
cocer.before('render', (req, res, next) => {
  const apiInfo = res.apiInfo;

  Object.keys(apiInfo).map(name => {
    const info = apiInfo[name];
    return log.info(info.method, info.api, info.consumeTime, 'ms');
  });
  log.warn('cost time: ', req.reqCircle.end());

  next();
});

app.use((req, res) => {
  res.status(404).send('404 not found!');
});

app.listen(8080, () => {
  const startInfo = 'server run at http://localhost:8080';

  log.info(startInfo);
});

module.exports = app;
<commit_msg>update invoker handle 500 error<commit_after>const log = require('t-log');
const express = require('express');
const coc = require('../index');

const app = express();

const cocer = coc(app, {
  interceptorDir: `${__dirname}/interceptors`,
  routerDir: `${__dirname}/routers`,
  viewDir: `${__dirname}/views`
});


// 查看阶段列表，每个阶段都可以用before,after处理
log.info(cocer.stageNames);

cocer.before('pageInfo', (req, res, next) => {
  req.reqCircle = log.time();
  next();
});
cocer.before('initHttpRequest', (req, res, next) => {
  let timeout = 5 * 1000;

  if (req.router && req.router.timeout) {
    timeout = req.router.timeout;
  }
  req.httpRequestConfig = { timeout };

  next();
});

// forward has been test
cocer.after('requestProxy', (req, res, next) => {
  const pathname = req.pathname;
  log.debug('param:', req.param);
  log.debug('path:', pathname);

  if (pathname === '/forward') {
    log.info(req.router, '********');
    res.forward('/proxy');
    // res.redirect('http://www.163.com');
    // res.status(200).send('hello 200 ok!');
    // next已经可以正确处理forward情况
    // 类似redirect，res.send等已经响应了服务器的情况
    next();
    return;
  }

  next();
});
cocer.before('render', (req, res, next) => {
  const apiInfo = res.apiInfo;

  Object.keys(apiInfo).map(name => {
    const info = apiInfo[name];
    return log.info(info.method, info.api, info.consumeTime, 'ms');
  });
  log.warn('cost time: ', req.reqCircle.end());

  next();
});

app.use((req, res) => {
  res.status(404).send('404 not found!');
});

app.use((error, req, res, next) => res.status(500).send(`<pre>${error.stack}</pre>`));

app.listen(8080, () => {
  const startInfo = 'server run at http://localhost:8080';

  log.info(startInfo);
});

module.exports = app;

{'fraction_non_alphanumeric': 0.21390374331550802, 'fraction_numerical': 0.0184194890077243, 'mean_word_length': 3.816881258941345, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4966506', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1251, 'n_words': 229}