-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (40 loc) · 1.22 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const express = require('express')
const app = express()
require('./db/conn')
const Users = require('./models/userSchema')
const Port = process.env.PORT || 5000
app.use(express.json())
app.post('/contact', async (req, res) => {
const { name, email, subject, message, date } = req.body
if (!name || !email || !subject || !message) {
return res.status(422).json({ err: 'error h bhai' })
}
try {
const newUser = new Users({ name, email, subject, message, date })
const userQuery = await newUser.save()
if (userQuery) {
res.status(200).json({ message: 'successful' })
} else {
res.status(422).json({ message: 'unsuccessful' })
}
if (!userQuery) {
console.log('error 422')
}
} catch (error) {
res.status(422)
console.log(`${error} h bahi`)
}
})
// 3 step
if (process.env.NODE_ENV === 'production') {
// Exprees will serve up production assets
app.use(express.static('client/build'))
// Express serve up index.html file if it doesn't recognize route
const path = require('path')
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
}
app.listen(Port, () => {
console.log(`Server is running at ${Port}`)
})