forked from bro-students/multy-stub
92 lines
2.6 KiB
JavaScript
92 lines
2.6 KiB
JavaScript
"use strict";
|
|
var __awaiter =
|
|
(this && this.__awaiter) ||
|
|
function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) {
|
|
return value instanceof P
|
|
? value
|
|
: new P(function (resolve) {
|
|
resolve(value);
|
|
});
|
|
}
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) {
|
|
try {
|
|
step(generator.next(value));
|
|
} catch (e) {
|
|
reject(e);
|
|
}
|
|
}
|
|
function rejected(value) {
|
|
try {
|
|
step(generator["throw"](value));
|
|
} catch (e) {
|
|
reject(e);
|
|
}
|
|
}
|
|
function step(result) {
|
|
result.done
|
|
? resolve(result.value)
|
|
: adopt(result.value).then(fulfilled, rejected);
|
|
}
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
var __importDefault =
|
|
(this && this.__importDefault) ||
|
|
function (mod) {
|
|
return mod && mod.__esModule ? mod : { default: mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const paths_1 = require("./paths");
|
|
const express_1 = __importDefault(require("express"));
|
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const path_1 = __importDefault(require("path"));
|
|
// Initialize express
|
|
const app = (0, express_1.default)();
|
|
// Load environment variables
|
|
dotenv_1.default.config();
|
|
// Parse JSON bodies
|
|
app.use(express_1.default.json());
|
|
|
|
// CORS
|
|
app.use((req, res, next) => {
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE");
|
|
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
next();
|
|
});
|
|
// Add the required directories
|
|
app.use((req, res, next) => {
|
|
const directories = ["/static", "/profiles"];
|
|
directories.forEach((dir) => {
|
|
if (!fs_1.default.existsSync(paths_1.BASE_PATH + dir)) {
|
|
fs_1.default.mkdirSync(paths_1.BASE_PATH + dir);
|
|
}
|
|
});
|
|
next();
|
|
});
|
|
// TODO: Remove this in production
|
|
app.use((req, res, next) => {
|
|
console.log(`${req.method} ${req.url}`);
|
|
next();
|
|
});
|
|
// Serve Static generated SVGs
|
|
app.get("/static/:name", (req, res, next) =>
|
|
__awaiter(void 0, void 0, void 0, function* () {
|
|
const fileName = req.params.name;
|
|
const filePath = `${paths_1.STATIC_PATH}/${fileName}`;
|
|
const file = yield fs_1.default.readFileSync(filePath);
|
|
res.setHeader("Content-Type", "image/svg+xml");
|
|
res.send(file);
|
|
})
|
|
);
|
|
app.use("/api", require("./routes/api").default);
|
|
|
|
// Start server
|
|
const port = process.env.PORT || 5000;
|
|
app.listen(port, () => {
|
|
console.log(`Server started on port ${port}`);
|
|
});
|