mirror of
https://github.com/sasjs/server.git
synced 2025-12-10 19:34:34 +00:00
66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import path from 'path'
|
|
import express, { ErrorRequestHandler } from 'express'
|
|
import morgan from 'morgan'
|
|
import cookieParser from 'cookie-parser'
|
|
import dotenv from 'dotenv'
|
|
import cors from 'cors'
|
|
|
|
import {
|
|
connectDB,
|
|
copySASjsCore,
|
|
getWebBuildFolderPath,
|
|
loadAppStreamConfig,
|
|
sasJSCoreMacros,
|
|
setProcessVariables
|
|
} from './utils'
|
|
|
|
dotenv.config()
|
|
|
|
const app = express()
|
|
|
|
const { MODE, CORS, WHITELIST } = process.env
|
|
|
|
if (MODE?.trim() !== 'server' || CORS?.trim() === 'enable') {
|
|
const whiteList: string[] = []
|
|
WHITELIST?.split(' ')?.forEach((url) => {
|
|
if (url.startsWith('http'))
|
|
// removing trailing slash of URLs listing for CORS
|
|
whiteList.push(url.replace(/\/$/, ''))
|
|
})
|
|
|
|
console.log('All CORS Requests are enabled for:', whiteList)
|
|
app.use(cors({ credentials: true, origin: whiteList }))
|
|
}
|
|
|
|
app.use(cookieParser())
|
|
app.use(morgan('tiny'))
|
|
app.use(express.json({ limit: '50mb' }))
|
|
app.use(express.static(path.join(__dirname, '../public')))
|
|
|
|
const onError: ErrorRequestHandler = (err, req, res, next) => {
|
|
console.error(err.stack)
|
|
res.status(500).send('Something broke!')
|
|
}
|
|
|
|
export default setProcessVariables().then(async () => {
|
|
await copySASjsCore()
|
|
|
|
// loading these modules after setting up variables due to
|
|
// multer's usage of process var process.driveLoc
|
|
const { setupRoutes } = await import('./routes/setupRoutes')
|
|
setupRoutes(app)
|
|
|
|
await loadAppStreamConfig()
|
|
|
|
// should be served after setting up web route
|
|
// index.html needs to be injected with some js script.
|
|
app.use(express.static(getWebBuildFolderPath()))
|
|
|
|
console.log('sasJSCoreMacros', sasJSCoreMacros)
|
|
|
|
app.use(onError)
|
|
|
|
await connectDB()
|
|
return app
|
|
})
|