My app.js file is as follows
var express = require('express');
var app = express();
var uuid = require('node-uuid');
var pg = require('pg');
var conString = process.env.DB; // "postgres://username:password@localhost/database";
// Routes
app.get('/api/status', function(req, res) {
pg.connect(conString, function(err, client, done) {
if(err) {
return res.status(500).send('error fetching client from pool');
}
client.query('SELECT now() as time', [], function(err, result) {
//call `done()` to release the client back to the pool
done();
if(err) {
return res.status(500).send('error running query');
}
return res.json({
request_uuid: uuid.v4(),
time: result.rows[0].time
});
});
});
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: {}
});
});
module.exports = app;
and my Dockerfile is as follows
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
my docker-compose file is as follows:
version: "3.2"
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: test123
POSTGRES_DB: testing
volumes:
- ./pgdata:/var/lib/postgresql/data
web:
build: .
command: npm start app.js
depends_on:
- db
environment:
DB: 'postgres://postgres:test123@localhost:5432/testing'
ports:
- "3000:3000"
When I am checking into the container DB, I can see the user, database but my app.js file cannot fetch a value from the database. I am routing the data in this endpoint /api/status when I am accessing http://ip-address:3000 == then i am getting 404 error which I mentioned in my code. without docker, I can easily access my output at the given endpoint.
Any help will be helpful. Thanksss