Created
July 19, 2023 22:30
-
-
Save miguelmota/939ab58fd1b5bdf354391be3d11be6ae to your computer and use it in GitHub Desktop.
TypeScript Node.js Express server example cors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cors from 'cors' | |
import express from 'express' | |
const app = express() | |
const port = process.env.PORT || 8000 | |
app.enable('trust proxy') | |
app.use(cors()) | |
app.use(express.json({ limit: '500kb' })) | |
app.use(express.urlencoded({ extended: false, limit: '500kb', parameterLimit: 50 })) | |
app.get('/', (req: any, res: any) => { | |
try { | |
res.status(200).json({ status: 'ok' }) | |
} catch (err: any) { | |
res.status(500).json({ error: err.message }) | |
} | |
}) | |
app.post('/', async (req: any, res: any) => { | |
try { | |
res.status(200).json({}) | |
} catch (err: any) { | |
res.status(500).json({ error: err.message }) | |
} | |
}) | |
app.get('/health', (req: any, res: any) => { | |
res.status(200).json({ status: 'ok' }) | |
}) | |
const host = '0.0.0.0' | |
app.listen(port, host, () => { | |
console.log(`Listening on port ${port}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment