skip to Main Content

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


  1. Chosen as BEST ANSWER

    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


    1. make sure the .env file is at the root of your project folder

    2. 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()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search