I following a tutorial and had this problem, but seems like it was only me.. Everytime i’m trying to run this i have the same output. Please help
im trying to connecting backend to the database
This is my server.js file
const PORT = process.env.PORT ?? 8000
const express = require('express')
const app = express()
const pool = require('./db')
//get all todos
app.get('/todos', async (req, res) => {
try {
const todos = await pool.query('SELECT * FROM todos')
res.json(todos.rows)
} catch (err) {
console.error(error)
}
})
app.listen(PORT, ( )=> console.log(`Server running on PORT ${PORT}`))
this is my db.js file
const Pool = require('pg').Pool
require ('dotenv').config()
const pool = new Pool({
user: process.env.USERNAME,
password: process.env.PASSWORD,
host: process.env.HOST,
port: process.env.DBPORT,
database: 'todoapp'
})
module.exports = pool
this is my data.sql file
CREATE DATABASE todoapp;
CREATE TABLE todos (
id VARCHAR(255) Primary key,
user_email VARCHAR(255),
title VARCHAR(30),
progress INT,
date VARCHAR(300)
);
CREATE TABLE users (
email VARCHAR(255) Primary Key,
hashed_password VARCHAR(255)
);
And then i have a file called ‘.env’ this the username, password, host and port
2
Answers
After help it stopped giving that nodemon error. Bu now its giving this one "error: password authentication failed for user "inesm"" however it is correct
make sure the .env file is at the root of your project folder
require the dotenv module and call the cofig method on it from your server.js file
it should look like this
const dotenv = require("dotenv")
dotenv.config()