I'm super new to express and node and I'm following a tutorial on how to make an inventory app. I'm also using postman to check the endpoints for the post request but getting this error:
SequelizeHostNotFoundError: getaddrinfo ENOTFOUND Database_HostName
server.js
const express = require('express');
const app = express();
const PORT = 8085;
//initialize routes
const categoriesManagement = require('./routes/categoriesManagement');
app.use('/api/categoriesmanagement', categoriesManagement);
app.listen(PORT);
console.log('server is running on http://127.0.0.1:' + PORT);
Credentials.js
// adding db connection as a constant
module.exports = Object.freeze({
userName: 'DATABASE_USERNAME',
password: 'DATABASE_PASSWORD',
hostName: 'Database_HostName',
databaseName: 'DATABASE_NAME',
});
CategoriesManagement.js
const express = require('express');
const router = express.Router();
const Sequelize = require('sequelize');
// method of sequelize to give us access to search filter
const { QueryTypes } = require('sequelize');
const credentials = require('../credentials');
const { response } = require('express');
const bodyParser = require('body-parser');
router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());
router.use(bodyParser.raw());
//initial sequelize to connect to db
const databaseConnection = new Sequelize(
credentials.databaseName,
credentials.userName,
credentials.password,
{
dialect: 'postgres',
host: credentials.hostName,
port: 5432, //db default port
logging: false,
dialectOptions: {
requestTimeout: 30000,
},
}
);
//define model
const Category = databaseConnection.define(
'Category',
{
categoryId: {
// automatically set primary key
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true,
},
categoryName: Sequelize.STRING,
isDeleted: Sequelize.BOOLEAN,
},
{
timestamps: false,
freezeTableName: true,
}
);
//create new category
router.post('/create_new_category', (req, res) => {
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
Category.create(req.body)
.then((category) => {
res.json(category);
})
.catch((err) => {
console.log('there was an error:' + err);
});
});
//test connection
router.get('/testconnection', (req, res) => {
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
res.send('API WORKS!');
});
module.exports = router;
However, my get request is working. I'm not sure what the issue is with the connection with the hostName.
question from:https://stackoverflow.com/questions/65942965/connecting-to-server-with-postgres-credentials-with-node-and-express