init datamate

This commit is contained in:
Dallas98
2025-10-21 23:00:48 +08:00
commit 1c97afed7d
692 changed files with 135442 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
const errorHandle = (err, req, res, next) => {
if(res.headersSent) {
return next(err);
}
console.error('Server Error:', err.message);
res.status(500).json({
code: '500',
msg: 'Internal Server Error',
data: null,
});
};
module.exports = errorHandle;

View File

@@ -0,0 +1,11 @@
const setHeader = require('./set-header-middleware.cjs');
const strongMatch = require('./strong-match-middleware.cjs');
const sendJSON = require('./send-json-middleawre.cjs');
const errorHandle = require('./error-handle-middleware.cjs');
module.exports = {
setHeader,
strongMatch,
sendJSON,
errorHandle,
};

View File

@@ -0,0 +1,18 @@
const sendJSON = (req, res, next) => {
res.sendJSON = (
data = null,
{ code = '0', msg = 'success', statusCode = 200, timeout = 0 } = {}
) => {
const timer = setTimeout(() => {
res.status(statusCode).json({
code,
msg,
data,
});
clearTimeout(timer);
}, timeout);
};
next();
};
module.exports = sendJSON;

View File

@@ -0,0 +1,14 @@
const setHeader = (req, res, next) => {
res.set({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Content-Security-Policy': "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src *; font-src 'self';",
'X-Content-Type-Options': 'nosniff',
'X-Frame-Options': 'SAMEORIGIN',
'X-XSS-Protection': '1; mode=block',
});
next();
};
module.exports = setHeader;

View File

@@ -0,0 +1,13 @@
const API = require('../mock-apis.cjs');
const strongMatch = (req, res, next) => {
res.strongMatch = () => {
const { url } = req;
const index = url.indexOf('?');
const targetUrl = index !== -1 ? url.substring(0, index) : url;
const isExistedUrl = Object.values(API).includes(targetUrl);
return isExistedUrl;
};
next();
};
module.exports = strongMatch;