LocationAPI/src/index.js

92 lines
2.8 KiB
JavaScript
Raw Normal View History

2025-04-01 18:32:44 +00:00
import file from 'node:fs'
import https from 'node:https'
import path from 'node:path'
import process from 'node:process'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc.js'
import express from 'express'
import config from './config/index.js'
import routes from './routes/index.js'
2025-05-14 07:11:07 +00:00
import router from './routes/uploadRoutes.js'
2025-04-01 18:32:44 +00:00
import { error404, errorHandler } from './utils/errors.js'
import { getRoutes } from './utils/links.js'
import { fileURLToPath } from 'node:url'
2025-04-07 22:10:37 +00:00
import { dirname } from 'node:path'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
2025-04-01 18:32:44 +00:00
const app = express()
dayjs.extend(utc)
const { NODE_ENV } = process.env
/*//Verify that upload directory exists
const uploadsDir = path.join(__dirname, 'uploads');
if (!file.existsSync(uploadsDir)) {
file.mkdirSync(uploadsDir, { recursive: true });
}*/
2025-04-01 18:32:44 +00:00
/**
* MIDDLEWARE------------------------------------------
*/
//parses incoming requests with JSON payloads
app.use(express.json({
limit: process.env.APP_BODY_LIMIT || config.bodyLimit
}));
/**
* BODY PARSER------------------------------------------
*/
//parses incoming requests with urlencoded payloads
//extended: false - parsing the URL-encoded data with the classic encoding algorithm
app.use(express.urlencoded({extended: false}));
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET,PATCH,PUT,POST,DELETE')
2025-04-01 18:32:44 +00:00
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Client-Version')
next()
})
// ROUTES --------------------------------------------------------------------------------
const r1 = routes(config)
2025-05-14 07:11:07 +00:00
const r2 = router(config)
2025-04-01 18:32:44 +00:00
const links1 = getRoutes({ prefix: '', routes: r1.stack })
const linkRoutes = { ...links1 }
2025-05-14 07:11:07 +00:00
const links2 = getRoutes({ prefix: '', routes: r2.stack })
const linkRoutes2 = { ...links2 }
2025-04-01 18:32:44 +00:00
2025-05-14 07:11:07 +00:00
app.use('/', r1, r2)
2025-04-01 18:32:44 +00:00
app.use('/public', express.static(path.join(__dirname, 'uploads')));
2025-04-01 18:32:44 +00:00
// APPLICATION LAUNCHER ------------------------------------------------------------------
// 404 - Not found
app.use((req, res) => {
const err = error404()
const error = errorHandler({ err, code: 'NOT_FOUND' }, config.environment)
res.status(error.code).json(error)
})
/*const httpsOptions = () => {
return {
key: file.readFileSync(path.join(__dirname, '..', 'key.pem')),
cert: file.readFileSync(path.join(__dirname, '..', 'cert.pem'))
}
}*/
const port = config.port || 3000
const server = app.listen(port, () => {
2025-04-06 16:37:49 +00:00
//eslint-disable-next-line no-console
2025-04-01 18:32:44 +00:00
console.log(`Server listening on port ${port}`)
})
/*const server = (NODE_ENV === 'development' ? app : https.createServer(httpsOptions(), app)).listen(port, () => {
//eslint-disable-next-line no-console
console.log(`Server listening on port ${port}`)
})*/
2025-05-14 07:11:07 +00:00
export { app, linkRoutes, linkRoutes2, server }