parent
86b4b9d523
commit
a9e153cfd6
@ -1,57 +1,57 @@
|
|||||||
const express = require("express");
|
const express = require("express");
|
||||||
const bodyParser = require('body-parser')
|
const bodyParser = require("body-parser");
|
||||||
const cache = require('../../napi/util/apicache').middleware
|
const cache = require("../../napi/util/apicache").middleware;
|
||||||
const fileUpload = require('express-fileupload')
|
const fileUpload = require("express-fileupload");
|
||||||
|
|
||||||
import routes from "../../napi/routes";
|
import routes from "../../napi/routes";
|
||||||
|
|
||||||
// Integrate API
|
// Integrate API
|
||||||
const app = express()
|
const app = express();
|
||||||
|
|
||||||
// CORS & Preflight request
|
// CORS & Preflight request
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
if (req.path !== '/' && !req.path.includes('.')) {
|
if (req.path !== "/" && !req.path.includes(".")) {
|
||||||
res.set({
|
res.set({
|
||||||
'Access-Control-Allow-Credentials': true,
|
"Access-Control-Allow-Credentials": true,
|
||||||
'Access-Control-Allow-Origin': req.headers.origin || '*',
|
"Access-Control-Allow-Origin": req.headers.origin || "*",
|
||||||
'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',
|
"Access-Control-Allow-Headers": "X-Requested-With,Content-Type",
|
||||||
'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',
|
"Access-Control-Allow-Methods": "PUT,POST,GET,DELETE,OPTIONS",
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
"Content-Type": "application/json; charset=utf-8",
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
req.method === 'OPTIONS' ? res.status(204).end() : next()
|
req.method === "OPTIONS" ? res.status(204).end() : next();
|
||||||
})
|
});
|
||||||
|
|
||||||
// cookie parser
|
// cookie parser
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
req.cookies = {}
|
req.cookies = {};
|
||||||
;(req.headers.cookie || '').split(/\s*;\s*/).forEach((pair) => {
|
(req.headers.cookie || "").split(/\s*;\s*/).forEach((pair) => {
|
||||||
let crack = pair.indexOf('=')
|
let crack = pair.indexOf("=");
|
||||||
if (crack < 1 || crack == pair.length - 1) return
|
if (crack < 1 || crack == pair.length - 1) return;
|
||||||
req.cookies[
|
req.cookies[
|
||||||
decodeURIComponent(pair.slice(0, crack)).trim()
|
decodeURIComponent(pair.slice(0, crack)).trim()
|
||||||
] = decodeURIComponent(pair.slice(crack + 1)).trim()
|
] = decodeURIComponent(pair.slice(crack + 1)).trim();
|
||||||
})
|
});
|
||||||
next()
|
next();
|
||||||
})
|
});
|
||||||
|
|
||||||
// body parser
|
// body parser
|
||||||
app.use(bodyParser.json())
|
app.use(bodyParser.json());
|
||||||
app.use(bodyParser.urlencoded({ extended: false }))
|
app.use(bodyParser.urlencoded({ extended: false }));
|
||||||
|
|
||||||
app.use(fileUpload())
|
app.use(fileUpload());
|
||||||
|
|
||||||
// cache
|
// cache
|
||||||
app.use(cache('2 minutes', (req, res) => res.statusCode === 200))
|
app.use(cache("2 minutes", (req, res) => res.statusCode === 200));
|
||||||
// router
|
// router
|
||||||
|
|
||||||
Object.keys(routes).forEach(route => {
|
Object.keys(routes).forEach((route) => {
|
||||||
app.use(route, routes[route])
|
app.use(route, routes[route]);
|
||||||
})
|
});
|
||||||
|
|
||||||
const port = process.env.PORT || 10754
|
const port = process.env.PORT || 10754;
|
||||||
const host = process.env.HOST || '127.0.0.1'
|
const host = process.env.HOST || "127.0.0.1";
|
||||||
|
|
||||||
app.server = app.listen(port, host, () => {
|
app.server = app.listen(port, host, () => {
|
||||||
console.log(`server running @ http://${host ? host : 'localhost'}:${port}`)
|
console.log(`server running @ http://${host ? host : "localhost"}:${port}`);
|
||||||
})
|
});
|
||||||
|
Loading…
Reference in new issue