Clean setup - nork v4, nothing else

This commit is contained in:
2024-04-29 21:26:04 +02:00
parent aa6643e023
commit 3f328e5316
8 changed files with 167 additions and 0 deletions

42
api/src/app.ts Normal file
View File

@ -0,0 +1,42 @@
import express from "express";
import morgan from "morgan";
//import path from 'path'
//import cors from 'cors'
//import cookieParser from 'cookie-parser'
import { router as routes } from "./routes";
//import { router as middlewares } from './middlewares'
//import env from './config/environment'
//export let corsWhitelist: Array<string>
//if (env.CORS_WHITELIST != 'undefined') {
// corsWhitelist = [...['http://localhost:8080', 'http://localhost:6040'], ...env.CORS_WHITELIST.split(';')]
//} else {
// corsWhitelist = ['http://localhost:8080', 'http://localhost:6040']
//}
//const corsOptions = {
// origin: function (origin: any, callback: any) {
// if (!origin || corsWhitelist.indexOf(origin) !== -1) {
// callback(null, true)
// } else {
// callback(new Error('Not allowed by CORS'))
// }
// },
// optionsSuccessStatus: 200,
// credentials: true
//}
export const app = express();
// Middlewares
//app.use(middlewares)
//app.set('view engine', 'ejs')
//app.set('views', path.join(__dirname, 'views'))
//app.use(cors(corsOptions))
app.use(morgan("dev"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
//app.use(express.static(path.join(__dirname, 'public')))
//app.use(cookieParser())
// Routes
app.use(routes);

View File

@ -0,0 +1,4 @@
import path from "path";
import { Env } from "nork";
const env = Env.get(path.join(__dirname, "../.env"));
export default env;

15
api/src/routes/index.ts Normal file
View File

@ -0,0 +1,15 @@
import { Request, Response, Router } from "express";
import path from "path";
//import authRoutes from "./authRoutes";
export const router = Router();
//router.use("/api/auth", authRoutes);
//router.get("*", (req: Request, res: Response) => {
// res.sendFile(path.join(__dirname, "../views/index.html"));
//});
// 404
router.use((req: Request, res: Response) => {
res.status(404).send("Error 404\n");
});

26
api/src/server.ts Normal file
View File

@ -0,0 +1,26 @@
import http from "http";
import { app } from "./app";
import env from "./config/environment";
//const env = {
// APP_PORT: 8080,
// APP_HOSTNAME: "127.0.0.1",
//};
import { Log } from "nork";
//import database from './config/database'
const port: number = env.APP_PORT || 8080;
const hostname: string = env.APP_HOSTNAME || "localhost";
export const server = http.createServer(app);
// Server
export function runServer(): void {
server.listen(port, hostname, () => {
Log.info(200, `Server is listening on http://${hostname}:${port}`);
});
}
//if (!env.NORK.database) {
runServer();
//} else {
// const db_connection = database()
// runServer()
//}