index.js 1005 Bytes
const MODULE_NAME = 'HTTP-SERVER';

const express = require('express');
const uniqid = require('uniqid');
const logger = require('tektrans-logger');

const config = require('../config');
const matrix = require('../matrix');
const checkApikey = require('./middlewares/check-apikey');

const routerMatrix = require('./routers/matrix');
const routerUpdates = require('./routers/updates');
const routerProducts = require('./routers/products');

const app = express();

app.use((req, res, next) => {
    matrix.httpServer.requestCounter += 1;
    res.locals.xid = uniqid();

    next();
});

app.use('/matrix', routerMatrix);
app.use('/apikey/:apikey/updates', [checkApikey], routerUpdates);
app.use('/apikey/:apikey/products', [checkApikey], routerProducts);

app.use((req, res) => {
    res.status(404).json({
        error: true,
        message: 'Method/service not found',
    });
});

const { port } = config;
app.listen(port, () => {
    logger.info(`${MODULE_NAME} 35069698: Listening`, { port });
});