3

I am newbie to Nodejs. I am creating apis using node, express and Postgres DB.

   const {Pool} = require('pg');



//const connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/springmvctutorial';

var http = require("http");
var express = require('express');
const router = express.Router();
const app = express();
var bodyParser = require('body-parser');

var apiVersion = '/api/v1/';


const poo = new Pool({
  database: 'springmvctutorial',
  host: 'localhost',
  user: 'postgres',
  password : 'root',
  port: 5432,
  max: 20,
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 2000,
})



app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
}));

//create app server
var server = app.listen(3030,  "localhost", function () {
  var host = server.address().address
  var port = server.address().port
  console.log("Example app listening at http://%s:%s", host, port)
});


router.post('/api/v1/postData', (req, res, next) => {

  poo.connect((err, client, release) => {
    if (err) {
      return console.error('Error acquiring client', err.stack);
    }
    client.query('INSERT INTO items(text, complete) values(1,false)', (err, result) => {
      release();
      if (err) {
        return console.error('Error executing query', err.stack);
      }
      console.log(result.rows);
    });
  });

});

module.exports = router;

My table and DB has been created. I checked from Postgres CLI. But when I am running my main.js file using node main.js. It starts listening to the port on the local host but when I fire,

   http://127.0.0.1:3030/api/v1/postData

It gives 404 resource not found error.

My input from postman is ,

{
"text":"[email protected]",
"complete": true
}
2
  • can you provide us with link of you are hitting and if it is post or get? did you try to hit it by localhost ? Commented Oct 17, 2017 at 9:41
  • I am trying from local host. It is post request. Commented Oct 17, 2017 at 9:47

1 Answer 1

2

try like this, i didn't test it due to i don't have the environment in this machine

const {Pool} = require('pg');
//const connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/springmvctutorial';
var http = require("http");
var express = require('express');
const app = express();
var bodyParser = require('body-parser');

var apiVersion = '/api/v1/';
const poo = new Pool({
  database: 'springmvctutorial',
  host: 'localhost',
  user: 'postgres',
  password : 'root',
  port: 5432,
  max: 20,
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 2000,
})
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
}));

//create app server
app.listen(3030,  "localhost", function () {
  var host = server.address().address
  var port = server.address().port
  console.log("Example app listening at http://%s:%s", host, port)
});


app.post('/api/v1/postData', (req, res, next) => {

  poo.connect((err, client, release) => {
    if (err) {
      return console.error('Error acquiring client', err.stack);
    }
    client.query('INSERT INTO items(text, complete) values(1,false)', (err, result) => {
      release();
      if (err) {
        return console.error('Error executing query', err.stack);
      }
      console.log(result.rows);
    });
  });

});
Sign up to request clarification or add additional context in comments.

3 Comments

No @Basil, It is not working.It is throwing ReferenceError: server is not defined. I think, we need var server to get address and port.
Well, i told you because i don't have the environment in this machine please remove var host = server.address().address var port = server.address().port they are useless now and let me know if do you have any update :)
After removing server referenced code. It worked. Can you please tell me why it was happening so that I will not repeat in future because the same code was working with MySql.(Server port listening code.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.