-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
47 lines (37 loc) · 1.25 KB
/
server.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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
// BodyParser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/myproject', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));
// Define a schema for the contact form
const Schema = mongoose.Schema;
const contactSchema = new Schema({
first_name: String,
last_name: String,
email_address: String,
message: String,
});
// Create a model from the schema
const Contact = mongoose.model('Contact', contactSchema);
// POST route to receive form submissions
app.post('/subscribe', async (req, res) => {
try {
const { first_name, last_name, email_address, message } = req.body;
const newContact = new Contact({ first_name, last_name, email_address, message });
await newContact.save();
res.status(201).send('Subscription successful');
} catch (err) {
res.status(500).send('Server error');
}
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));