oneuptime/http-test-server/server.js

106 lines
3.0 KiB
JavaScript
Raw Normal View History

2021-01-26 14:03:42 +00:00
process.on('exit', () => {
/* eslint-disable no-console */
console.log('Shutting Shutdown');
});
process.on('unhandledRejection', err => {
/* eslint-disable no-console */
console.error('Unhandled rejection in process occurred');
/* eslint-disable no-console */
console.error(err);
});
process.on('uncaughtException', err => {
/* eslint-disable no-console */
console.error('Uncaught exception in process occurred');
/* eslint-disable no-console */
console.error(err);
});
2020-02-16 23:18:38 +00:00
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const http = require('http');
2020-02-16 23:18:38 +00:00
const { NODE_ENV } = process.env;
if (NODE_ENV === 'local' || NODE_ENV === 'development')
require('custom-env').env(process.env.NODE_ENV);
global.httpServerResponse = {
statusCode: 200,
2020-02-27 11:15:46 +00:00
responseType: { values: ['json', 'html'], currentType: 'json' },
responseTime: 0,
header: {},
2020-02-27 11:15:46 +00:00
body: { status: 'ok' },
};
2020-10-19 02:08:52 +00:00
app.use('*', function(req, res, next) {
if (process.env && process.env.PRODUCTION) {
res.set('Cache-Control', 'public, max-age=86400');
2020-02-27 11:15:46 +00:00
} else res.set('Cache-Control', 'no-cache');
next();
});
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
2020-02-29 22:56:00 +00:00
app.use(bodyParser.json());
app.use(require('./backend/api/settings'));
2020-10-19 02:08:52 +00:00
app.get('/', function(req, res) {
if (http.STATUS_CODES[global.httpServerResponse.statusCode]) {
res.status(global.httpServerResponse.statusCode);
} else {
res.status(422);
}
2020-10-19 02:08:52 +00:00
setTimeout(function() {
if (global.httpServerResponse.responseType.currentType === 'html') {
2020-01-21 17:00:35 +00:00
res.setHeader('Content-Type', 'text/html');
try {
const header = JSON.parse(global.httpServerResponse.header);
if (typeof header === 'object') {
for (const key in header) {
res.setHeader(String(key), String(header[key]));
}
}
} catch (e) {
//
}
return res.send(global.httpServerResponse.body);
} else {
res.setHeader('Content-Type', 'application/json');
2020-01-21 17:00:35 +00:00
return res.send(global.httpServerResponse.body);
}
}, global.httpServerResponse.responseTime);
});
const hook = {};
2020-10-19 02:08:52 +00:00
app.post('/api/webhooks/:id', function(req, res) {
const { id } = req.params;
hook[id] = req.body;
return res.status(200).json(req.body);
});
2020-10-19 02:08:52 +00:00
app.get('/api/webhooks/:id', function(req, res) {
const { id } = req.params;
if (hook[id] === undefined) return res.status(404).json({});
return res.status(200).json(hook[id]);
});
2020-10-19 02:08:52 +00:00
app.use('/*', function(req, res) {
res.status(404).render('notFound.ejs', {});
});
app.set('port', process.env.PORT || 3010);
2020-10-19 02:08:52 +00:00
app.listen(app.get('port'), function() {
//eslint-disable-next-line
2020-02-27 11:15:46 +00:00
console.log('Server running on port : ' + app.get('port'));
});