Welcome to a demo of Express + PostSQL + Basic Auth.
-
Add your PostgreSQL database configurations to
modules/db/dbconfig.json
. -
Run the app using:
npm run dev
- Check if the app connected successfully to the database.
module/db/index.js
logs the status of the database connection:
const pgPromise = require('pg-promise');
const dbConfig = require('./dbconfig.json');
const { Users } = require('./repos');
const initOptions = {
extend(obj, dc) {
obj.users = new Users(obj, pgp);
}
}
// Initializing the library;
const pgp = pgPromise(initOptions);
// Creating the database instance;
const db = pgp(dbConfig);
// Test connection
db.one('SELECT NOW()')
.then(result => {
console.log('Connection successful:', result);
})
.catch(error => {
console.error('Connection failed:', error.message || error);
});
module.exports = {db};
http://localhost:3000/auth/signup
// Request Body
{
"username": "joe",
"password": "password"
}
// Request Body
{
"username": "joe",
"password": "password"
}
// Request Body
-
PostgreSQL Documentation: The official docs for PostgreSQL, the powerful open-source relational database.
-
pg-promise: A PostgreSQL client library for Node.js with a clean promise-based interface. Great for use with Express.
-
Passport.js: User Authentication.