0% found this document useful (0 votes)
24 views3 pages

Program No 12

Uploaded by

Rani Marri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Program No 12

Uploaded by

Rani Marri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

App.

js

var express = require('express');

var path = require('path');

var bodyParser = require('body-parser');

var mongodb = require('mongodb').MongoClient;

var app = express();

var url= "mongodb://localhost:27017/contact";

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

app.use(express.static(path.resolve(__dirname, 'public')));

app.post('/post-feedback', function (req, res) {

let contactMessage = {

name: req.body.name,

email: req.body.email,

message: req.body.message

};

mongodb.connect(url, function(err, db) {

var collection=db.collection('feedbacks')

collection.insert(contactMessage,function(err, res) {

console.log('message inserted');

db.close();

});

});

res.send('Data received:\n' + JSON.stringify(req.body));

});

app.get('/view-feedbacks', function(req, res) {


mongodb.connect().then(function(db) {

db.collection('feedbacks').find({}).toArray().then(function(feedbacks) {

res.status(200).json(feedbacks);

console.log(feedbacks)

});

});

});

app.listen(process.env.PORT || 3000, process.env.IP || '0.0.0.0' );

index.html

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Client Data</title>

</head>

<body>

<h1>Please fill data in the form below:</h1>

<form method="POST" action="/post-feedback">

<label>Name:<input type="text" name="client-name" required></label>

<br>

<label>Email:<input type="text" name="client-email" required></label>

<br>

<label>Comment:<br><textarea name="message"></textarea></label>

<br>

<input type="submit" value="Submit">


</form>

<a href="/view-feedbacks">View feedbacks</a>

</body>

</html>

You might also like