Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.2 kB
3
Indexable
Never
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');

const app = express();

// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/farmersDB', { useNewUrlParser: true, useUnifiedTopology: true });

// Create a schema for farmer details
const farmerSchema = new mongoose.Schema({
    email: String,
    name: String,
    phone: String
});

const Farmer = mongoose.model('Farmer', farmerSchema);

app.use(bodyParser.urlencoded({ extended: true }));

// Route for farmer registration
app.route('/register')
    .get((req, res) => {
        // Display registration form
        res.sendFile(__dirname + '/registration_form.html');
    })
    .post((req, res) => {
        // Handle farmer registration
        const newFarmer = new Farmer({
            email: req.body.email,
            name: req.body.name,
            phone: req.body.phone
        });

        newFarmer.save((err) => {
            if (err) {
                res.send('Error registering farmer.');
            } else {
                res.send('Registration successful!');
            }
        });
    });

app.listen(3000, function () {
    console.log('Server started on port 3000');
});
Leave a Comment