app.js 1.43 KB
'use strict';

const path = require('path');
const express = require('express');
const hbs = require('express-handlebars');
const config = require('./config/config');
const yohoLib = require('yoho-node-lib');

// 全局注册library
yohoLib.global(config);
global.yoho.redis = require('./libs/redis');
const logger = global.yoho.logger;
const app = express();
const seo = require('./apps/seo');
const cseo = require('./apps/controllers/seo/index');

// hbs-set
app.engine('hbs', hbs({
    extname: '.hbs',
    defaultLayout: 'layout',
    layoutsDir: path.join(__dirname, 'hbs'),
    partialsDir: path.join(__dirname, 'hbs/partials'),
    views: path.join(__dirname, 'apps/views')
}));

// set-express-view
app.set('views', path.join(__dirname, 'apps/views'));
app.set('view engine', 'hbs');

// 添加请求上下文
app.use(global.yoho.httpCtx());

app.get('/goods-:page-:limit.xml', cseo.index);
app.get('/demo-xml', cseo.demoXml);

// 定时任务 主动推送和生成xml
seo.start();

// seo.sendKeywordsUrls();
app.get('/synchronousKeywords', seo.synchronousKeywords);
app.get('/sendKeywordsUrls', seo.sendKeywordsUrls);
app.get('/sendUrls', seo.sendUrls);

// 404错误
app.use(function(req, res) {
    return res.status(404).send('');
});

// 500错误
app.use(function(err, req, res) {
    return res.status(err.status || 500).send('');
});


app.listen(config.port, function() {
    logger.info(`yoho seo start : http://127.0.0.1:${config.port}`);
});