Browse Source

api(WIP): sets routes and controllers for form handling

master
creton32 2 months ago
parent
commit
87ae7d1efa
  1. 7
      api/controllers/emailController.js
  2. 7
      api/routes/email.js
  3. 14
      api/routes/index.js
  4. 6
      api/server.js

7
api/controllers/emailController.js

@ -0,0 +1,7 @@
const { body, validationResult } = require('express-validator')
const sendEmail = (req, res) => {
// handle email sending logic
}
module.exports = { sendEmail }

7
api/routes/email.js

@ -0,0 +1,7 @@
const express = require('express')
const router = express.Router()
const { sendEmail } = require('../controllers/emailController')
router.get('/', sendEmail)
module.exports = router

14
api/routes/index.js

@ -0,0 +1,14 @@
const app = require('express')()
const emailRoutes = require('./email')
app.get('/', (req, res) => {
res.status(200).json({ message: 'API ready to take requests' })
})
app.use('/email', emailRoutes)
app.all('*', (req, res) => {
res.status(404).json({ message: 'Endpoint not found' })
})
module.exports = app

6
api/server.js

@ -1,5 +1,6 @@
const path = require('path')
const express = require('express')
const api = require('./routes/')
// Constants
const PORT = process.env.PORT || 4000
@ -9,13 +10,16 @@ const CLIENT_BUILD_PATH = path.join(__dirname, '../dist')
// Initiallize Express
const app = express()
// Build API
app.use('/api', api)
// Static files middleware
app.use(express.static(CLIENT_BUILD_PATH))
app.use('*', express.static(CLIENT_BUILD_PATH))
app.get('*', (req, res) => {
res.sendFile(path.join(CLIENT_BUILD_PATH, 'index.html'))
})
app.listen(PORT, HOST, () =>
console.log(`App running in http://${HOST}:${PORT}`)
)
Loading…
Cancel
Save