mirror of
https://github.com/xfarrow/blink
synced 2025-06-06 00:49:11 +02:00
Update api.js
This commit is contained in:
parent
46d88b06a9
commit
022a85e211
@ -3,24 +3,25 @@
|
|||||||
** licensed under GPLv3
|
** licensed under GPLv3
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const { Client } = require('pg');
|
|
||||||
const bcrypt = require('bcrypt');
|
const bcrypt = require('bcrypt');
|
||||||
|
const { Pool } = require('pg');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
|
|
||||||
|
|
||||||
// Middleware which parses JSON for POST requests
|
// Middleware which parses JSON for POST requests
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
// Sample data (an array of items)
|
const pool = new Pool({
|
||||||
const items = [
|
user: 'postgres',
|
||||||
{ id: 1, name: 'Item 1' },
|
host: 'localhost',
|
||||||
{ id: 2, name: 'Item 2' },
|
database: 'Blink',
|
||||||
{ id: 3, name: 'Item 3' },
|
password: 'postgres',
|
||||||
];
|
port: 5432,
|
||||||
|
max: 10,
|
||||||
|
idleTimeoutMillis: 30000,
|
||||||
|
});
|
||||||
|
|
||||||
// Define a route to get all items
|
// Define a route to get all items
|
||||||
app.get('/api/items', (req, res) => {
|
app.get('/api/items', (req, res) => {
|
||||||
@ -32,25 +33,21 @@ app.post('/api/register', (req, res) => {
|
|||||||
const userData = req.body;
|
const userData = req.body;
|
||||||
|
|
||||||
// Ensure that the required fields are present before proceeding
|
// Ensure that the required fields are present before proceeding
|
||||||
if (!userData.display_name || !userData.email) {
|
if (!userData.display_name || !userData.email || !userData.password) {
|
||||||
return res.status(400).json("Invalid request");
|
return res.status(400).json("Invalid request");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a PostgreSQL client
|
bcrypt.hash(userData.password, 10, (err, hashedPassword) => {
|
||||||
const client = new Client({
|
if (err) {
|
||||||
user: 'postgres',
|
console.error('Error hashing password:', err);
|
||||||
host: 'localhost',
|
} else {
|
||||||
database: 'Blink',
|
// Acquire a connection from the pool
|
||||||
password: 'postgres',
|
pool.connect()
|
||||||
port: 5432, // Default PostgreSQL port
|
.then((client) => {
|
||||||
});
|
|
||||||
|
|
||||||
client.connect()
|
|
||||||
.then(() => {
|
|
||||||
// SQL query with placeholders for parameters
|
// SQL query with placeholders for parameters
|
||||||
const insertQuery = `
|
const insertQuery = `
|
||||||
INSERT INTO "User" (display_name, date_of_birth, place_of_living, is_looking_for_job, email)
|
INSERT INTO "User" (display_name, date_of_birth, place_of_living, is_looking_for_job, email, password)
|
||||||
VALUES ($1, $2, $3, $4, $5)
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
RETURNING *`; // Return the inserted row
|
RETURNING *`; // Return the inserted row
|
||||||
|
|
||||||
return client.query(insertQuery, [
|
return client.query(insertQuery, [
|
||||||
@ -58,9 +55,9 @@ app.post('/api/register', (req, res) => {
|
|||||||
userData.date_of_birth,
|
userData.date_of_birth,
|
||||||
userData.place_of_living,
|
userData.place_of_living,
|
||||||
userData.is_looking_for_job,
|
userData.is_looking_for_job,
|
||||||
userData.email
|
userData.email,
|
||||||
]);
|
hashedPassword
|
||||||
})
|
])
|
||||||
.then((result) => {
|
.then((result) => {
|
||||||
// Respond with the inserted user data
|
// Respond with the inserted user data
|
||||||
res.status(200).json(result.rows[0]);
|
res.status(200).json(result.rows[0]);
|
||||||
@ -70,8 +67,15 @@ app.post('/api/register', (req, res) => {
|
|||||||
res.status(500).json("Internal server error");
|
res.status(500).json("Internal server error");
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
// Close the database connection
|
// Release the connection back to the pool
|
||||||
client.end();
|
client.release();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error('Error acquiring a connection from the pool:', error);
|
||||||
|
res.status(500).json("Internal server error");
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user