I need to change my database from a mongo to a PostgreSQL server. Anything would
ID: 3875828 • Letter: I
Question
I need to change my database from a mongo to a PostgreSQL server. Anything would help thanks.
SERVER CODE:
console.log('Server-side code running');
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const app = express();
app.use(express.static('public'));
let db;
const url = 'mongodb://localhost:27017/clicks';
MongoClient.connect(url, (err, database) => {
if (err) {
return console.log(err);
}
db = database;
// start the express web server listening on 8080
app.listen(8080, () => {
console.log('listening on 8080');
});
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.post('/clicked', (req, res) => {
db.collection('clicks').findOneAndUpdate({}, {$inc: {click : 1}}, (err, result) => {
if(err) throw err;
res.send(result);
});
});
//console.log(click);
//console.log(db);
// db.collection('clicks').save(click, (err, result) => {
// if (err) {
// return console.log(err);
// }
// console.log('click added to db');
// res.sendStatus(201);
// });
// });
app.get('/clicks', (req, res) => {
db.collection('clicks').findOne({ "myId" : "skylar" },function(err, result){
if (err) return console.log(err);
res.end(result.click+'');
});
});
CLIENT CODE:
console.log('Client-side code running');
const button = document.getElementById('myButton');
button.addEventListener('click', function(e) {
console.log('button was clicked');
fetch('/clicked', {method: 'POST'})
.then(function(response) {
if(response.ok) {
console.log('click was recorded');
return;
}
throw new Error('Request failed.');
})
.catch(function(error) {
console.log(error);
});
});
setInterval(function() {
fetch('/clicks', {method: 'GET'})
.then(function(response) {
if(response.ok) return response.json();
throw new Error('Request failed.');
})
.then(function(data) {
document.getElementById('counter').innerHTML = `Button was clicked ${data} times`;
})
.catch(function(error) {
console.log(error);
});
}, 1000);
HTML CODE:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Node + Express + MongoDb</title>
<style>
h1 {
text-align: center;
}
p {
text-align: center;
}
.button-center {
text-align: center;
padding-top: 50px;
}
#myButton {
height: 50px;
width: 150px;
border: 2px solid black;
}
</style>
</head>
<body>
<h1 id="counter">Loading button click data.</h1>
<div class="button-center">
<button id="myButton">The Button</button>
</div>
</body>
<script src="client.js"></script>
</html>
Explanation / Answer
To change the database from MongoDb to PostGreSql, you need to change the server code. The client code and HTML remains the same.
Please find below the server code using PostGreSql database:
console.log('Server-side code running');
const express = require('express');
const postgreSQLclient = require('pg-promise')({}); //Created an instance of postgresql
const app = express();
app.use(express.static('public'));
let db;
//Provide the correct url of your postgresql database in the string in next line
const url = 'postgresql://localhost:27017/clicks';
db = postgreSQLclient (url); //Connecting to the database
// start the express web server listening on 8080
app.listen(8080, () => {
console.log('listening on 8080');
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
var id = ‘skylar’; //A variable to store the id value. Used in search and update on database below
//Update click value by adding 1 in database for id = skylar, then return click value
app.post('/clicked', (req, res) => {
db.one('update clicks set click=click+1 where myId=$1 returning *',id)
.then(function (data) {
res.send(data.click);
})
.catch(function (error) {
return error;
});
});
//Select click value from database for id = skylar, then return click value
app.get('/clicks', (req, res) => {
db.one('select * from clicks where myId = $1’, id)
.then(function (data) {
res.send(data.click);
})
.catch(function (error) {
return error;
})
});
});
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.